2023-08-09 21:11:28 +09:00
|
|
|
#include <cstdio>
|
|
|
|
|
2023-08-09 15:11:42 +09:00
|
|
|
#include "integral.h"
|
|
|
|
|
2023-08-09 21:11:28 +09:00
|
|
|
#define CHECK_CUDA(call) \
|
|
|
|
do { \
|
|
|
|
cudaError_t status_ = call; \
|
|
|
|
if (status_ != cudaSuccess) { \
|
|
|
|
fprintf(stderr, "CUDA error (%s:%d): %s:%s\n", __FILE__, __LINE__, \
|
|
|
|
cudaGetErrorName(status_), cudaGetErrorString(status_)); \
|
|
|
|
exit(EXIT_FAILURE); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2023-08-12 16:20:29 +09:00
|
|
|
// Device(GPU) pointers
|
|
|
|
static double *pi_gpu;
|
2023-08-09 15:11:42 +09:00
|
|
|
|
2023-08-12 16:20:29 +09:00
|
|
|
static double f(double x) { return 4.0 / (1 + x * x); }
|
2023-08-09 21:11:28 +09:00
|
|
|
|
2023-08-12 16:20:29 +09:00
|
|
|
double integral_cpu(size_t num_intervals) {
|
|
|
|
double dx, sum;
|
|
|
|
dx = (1.0 / (double) num_intervals);
|
|
|
|
sum = 0.0f;
|
|
|
|
for (size_t i = 0; i < num_intervals; i++) { sum += f(i * dx) * dx; }
|
|
|
|
return sum;
|
2023-08-09 21:11:28 +09:00
|
|
|
}
|
2023-08-09 15:11:42 +09:00
|
|
|
|
2023-08-12 16:20:29 +09:00
|
|
|
double integral(size_t num_intervals) {
|
|
|
|
double pi_value = 0.0;
|
|
|
|
// Remove this line after you complete the matmul on GPU
|
|
|
|
pi_value = integral_cpu(num_intervals);
|
|
|
|
|
|
|
|
// (TODO) Launch kernel on a GPU
|
2023-08-09 21:11:28 +09:00
|
|
|
|
2023-08-12 16:20:29 +09:00
|
|
|
// (TODO) Download sum_gpu from GPU
|
2023-08-09 21:11:28 +09:00
|
|
|
|
2023-08-12 16:20:29 +09:00
|
|
|
// DO NOT REMOVE; NEED FOR TIME MEASURE
|
2023-08-09 21:11:28 +09:00
|
|
|
CHECK_CUDA(cudaDeviceSynchronize());
|
|
|
|
|
2023-08-12 16:20:29 +09:00
|
|
|
return pi_value;
|
2023-08-09 15:11:42 +09:00
|
|
|
}
|
|
|
|
|
2023-08-12 16:20:29 +09:00
|
|
|
void integral_init() {
|
|
|
|
// (TODO) Allocate device memory
|
|
|
|
|
|
|
|
// DO NOT REMOVE; NEED FOR TIME MEASURE
|
|
|
|
CHECK_CUDA(cudaDeviceSynchronize());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void integral_finalize() {
|
2023-08-09 21:11:28 +09:00
|
|
|
// (TODO) Free device memory
|
|
|
|
|
2023-08-12 16:20:29 +09:00
|
|
|
// DO NOT REMOVE; NEED FOR TIME MEASURE
|
2023-08-09 21:11:28 +09:00
|
|
|
CHECK_CUDA(cudaDeviceSynchronize());
|
|
|
|
}
|