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 9186415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:31:19+00:00 2026-06-17T19:31:19+00:00

This the CUDA code I want to calculate the elapsed time. I am pretty

  • 0

This the CUDA code I want to calculate the elapsed time. I am pretty new to CUDA so went and tried some API’s like .

      cudaEventRecord(stop, 0);
      cudaEventSynchronize(stop);
      float elapsedTime;
      cudaEventElapsedTime(&elapsedTime, start, stop);

But I dont know to put these statements in below code i.e I dont how to arrange these codes . Can anyone guide me where can insert these statements in the code ?

        #include<stdio.h>
        #define N 512

     __global__ void add( int *a, int *b, int *c) 
       {
               *c= *a+ *b;
       }

      int main(void) 
       {
        int *a, *b, *c; // host copies of a, b, c
        int *dev_a, *dev_b, *dev_c; // device copies of a, b, c
        int size = N * sizeof(int); // we need space for 512 integers


        // allocate device copies of a, b, c
        cudaMalloc( (void**)&dev_a, size );
        cudaMalloc( (void**)&dev_b, size );
        cudaMalloc( (void**)&dev_c, size );

        a = (int*)malloc( size );
        b = (int*)malloc( size );
        c = (int*)malloc( size );

        //random_ints( a, N );
        //random_ints( b, N );

        // copy inputs to device
        cudaMemcpy( dev_a, a, size, cudaMemcpyHostToDevice);
        cudaMemcpy( dev_b, b, size, cudaMemcpyHostToDevice);

        // launch add() kernel with N parallel blocks
        add <<< N, 1 >>>( dev_a, dev_b, dev_c);

        // copy device result back to host copy of c
        cudaMemcpy( c, dev_c, size, cudaMemcpyDeviceToHost);

        free( a ); free( b ); free( c );

        cudaFree( dev_a);
        cudaFree( dev_b);
        cudaFree( dev_c);


      }
  • 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-06-17T19:31:21+00:00Added an answer on June 17, 2026 at 7:31 pm

    Timing of CUDA events is measured as follows:

    Lets say you want to measure the time of the kernel add.

    cudaEvent_t start,stop;
    float elapsedTime;
    
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    cudaEventRecord(start,0);
    
    add <<< N, 1 >>>( dev_a, dev_b, dev_c);
    
    cudaEventRecord(stop,0);
    cudaEventSynchronize(stop);
    
    
    cudaEventElapsedTime(&elapsedTime,start,stop);
    
    cout<<"\n\nElapsed Time = "<<elapsedTime<<" milliseconds";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to instantiate a class in CUDA code, that shares some of
I have some code that I want to make into a cuda kernel. Behold:
I ran into this very weird problem when coding up some CUDA code: the
I have a data object that looks like this: { 'node-16': { 'tags': ['cuda'],
I found this code in NVIDIA's CUDA SDK samples. void computeGold( float* reference, float*
I have some CUDA code I made in C and it seems to be
I would like to organise my CUDA code into separate object files to be
I want to port my c code to CUDA. The main computational part contains
I have been trying to figure this out for quite some time. I use
I'm going to attempt to optimize some code written in MATLAB, by using CUDA.

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.