diff --git a/src/cuda.cu b/src/cuda.cu index 1c994f6c12b1e61a118902204eebbc95d2b78079..3b1a043ca2360978a4344c836c95a6a7b2718234 100644 --- a/src/cuda.cu +++ b/src/cuda.cu @@ -1,6 +1,7 @@ %%writefile test.cu #include <stdlib.h> #include <stdio.h> +#include "time.h" #define NMAX 100 #define DATAMAX 1000 @@ -213,8 +214,25 @@ void print_array(int *n, int size) printf("\n"); } + +// call this function to start a nanosecond-resolution timer +struct timespec timer_start(){ + struct timespec start_time; + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time); + return start_time; +} + +// call this function to end a timer, returning nanoseconds elapsed as a long +double timer_end(struct timespec start_time){ + struct timespec end_time; + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time); + double diffInNanos = (end_time.tv_sec - start_time.tv_sec) * (double)1e9 + (end_time.tv_nsec - start_time.tv_nsec); + return diffInNanos; +} + int main(int argc, char **argv){ - int num_targets = 3; + struct timespec vartime = timer_start(); + int num_targets; int rowA,colA; int rowB,colB; @@ -271,5 +289,8 @@ int main(int argc, char **argv){ printf("MEDIAN : %d \n",get_median(c,num_targets)); printf("AVERAGE : %ld \n",get_floored_mean(c,num_targets)); + double time_elapsed_nanos = timer_end(vartime)/1000000000; + printf("Time taken (second): %f \n", time_elapsed_nanos); + cudaDeviceReset(); } \ No newline at end of file