chundoong-lab-ta/APSS23/integral/integral.cu

56 lines
1.6 KiB
Plaintext
Raw Normal View History

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 17:22:36 +09:00
double integral_naive(size_t num_intervals) {
2023-08-12 16:20:29 +09:00
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
2023-08-12 17:22:36 +09:00
pi_value = integral_naive(num_intervals);
2023-08-12 16:20:29 +09:00
// (TODO) Launch kernel on a GPU
2023-08-09 21:11:28 +09:00
2023-08-12 17:22:36 +09:00
// (TODO) Download pi_value 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());
}