chundoong-lab-ta/APWS23/sum-reduction/main.cpp

37 lines
990 B
C++

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include "reduction.h"
#include "util.h"
int main(int argc, char **argv) {
size_t num_elements = 100000000;
if (argc > 1) sscanf(argv[1], "%lu", &num_elements);
printf("Number of elements: %lu\n", num_elements);
double *A = (double *) malloc(num_elements * sizeof(double));
rand_vec(A, num_elements);
double cpu_st, cpu_en, gpu_st, gpu_en;
cpu_st = get_current_time();
double cpu_pi_estimate = reduction_cpu(A, num_elements);
cpu_en = get_current_time();
reduction_gpu_initialize(num_elements);
gpu_st = get_current_time();
double gpu_pi_estimate = reduction_gpu(A, num_elements);
gpu_en = get_current_time();
reduction_gpu_finalize();
printf("Reduction result from CPU: %.16f\n", cpu_pi_estimate);
printf("Reduction result from GPU: %.16f\n", gpu_pi_estimate);
printf("CPU elapsed time: %.3f sec\n", cpu_en - cpu_st);
printf("GPU elapsed time: %.3f sec\n", gpu_en - gpu_st);
return 0;
}