Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6676627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:00:49+00:00 2026-05-26T04:00:49+00:00

I am looking for the most concise amount of code possible that can be

  • 0

I am looking for the most concise amount of code possible that can be coded both for a CPU (using g++) and a GPU (using nvcc) for which the GPU consistently outperforms the CPU. Any type of algorithm is acceptable.

To clarify: I’m literally looking for two short blocks of code, one for the CPU (using C++ in g++) and one for the GPU (using C++ in nvcc) for which the GPU outperforms. Preferably on the scale of seconds or milliseconds. The shortest code pair possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:00:49+00:00Added an answer on May 26, 2026 at 4:00 am

    First off, I’ll reiterate my comment: GPUs are high bandwidth, high latency. Trying to get the GPU to beat a CPU for a nanosecond job (or even a millisecond or second job) is completely missing the point of doing GPU stuff. Below is some simple code, but to really appreciate the performance benefits of GPU, you’ll need a big problem size to amortize the startup costs over… otherwise, it’s meaningless. I can beat a Ferrari in a two foot race, simply because it take some time to turn the key, start the engine and push the pedal. That doesn’t mean I’m faster than the Ferrari in any meaningful way.

    Use something like this in C++:

      #define N (1024*1024)
      #define M (1000000)
      int main()
      {
         float data[N]; int count = 0;
         for(int i = 0; i < N; i++)
         {
            data[i] = 1.0f * i / N;
            for(int j = 0; j < M; j++)
            {
               data[i] = data[i] * data[i] - 0.25f;
            }
         }
         int sel;
         printf("Enter an index: ");
         scanf("%d", &sel);
         printf("data[%d] = %f\n", sel, data[sel]);
      }
    

    Use something like this in CUDA/C:

      #define N (1024*1024)
      #define M (1000000)
    
      __global__ void cudakernel(float *buf)
      {
         int i = threadIdx.x + blockIdx.x * blockDim.x;
         buf[i] = 1.0f * i / N;
         for(int j = 0; j < M; j++)
            buf[i] = buf[i] * buf[i] - 0.25f;
      }
    
      int main()
      {
         float data[N]; int count = 0;
         float *d_data;
         cudaMalloc(&d_data, N * sizeof(float));
         cudakernel<<<N/256, 256>>>(d_data);
         cudaMemcpy(data, d_data, N * sizeof(float), cudaMemcpyDeviceToHost);
         cudaFree(d_data); 
    
         int sel;
         printf("Enter an index: ");
         scanf("%d", &sel);
         printf("data[%d] = %f\n", sel, data[sel]);
      }
    

    If that doesn’t work, try making N and M bigger, or changing 256 to 128 or 512.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the following query and results, I'm looking for the most recent entry where
I am looking for the most direct way to make it so that when
I'm looking for most performing Java service wrapper, which could make Java application running
I'm looking for the most Rails-y way to create a table that displays data
When looking at most sites (including SO), most of them use: <input type=button />
I'm looking for most elegant way to ajaxify my forms (with jQuery). How do
I'm looking for the most ideal data structure (for performance and ease of use)
I am looking for the most standard way to achieve modal dialogs in ASP.NET
I'm looking for the most basic solution to create multiple indexes on a Java
I am looking for a most efficient way to re-use widgets with different selectors...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.