chundoong-lab-ta/APWS23/ans/pythagoras_ans.cu

58 lines
1.8 KiB
Plaintext
Raw Normal View History

2023-02-14 20:17:53 +09:00
#include <cstdio>
2023-02-15 01:33: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); \
} \
2023-02-14 20:17:53 +09:00
} while (0)
2023-02-15 01:33:28 +09:00
__global__ void pythagoras(int *pa, int *pb, int *pc, int *pd) {
2023-02-14 20:17:53 +09:00
int a = *pa;
int b = *pb;
int c = *pc;
if ((a * a + b * b) == c * c)
*pd = 1;
else
*pd = 0;
}
2023-02-15 01:33:28 +09:00
int main(int argc, char *argv[]) {
2023-02-14 20:17:53 +09:00
if (argc != 4) {
printf("Usage: %s <num 1> <num 2> <num 3>\n", argv[0]);
return 0;
}
// TODO
int *d_a, *d_b, *d_c, *d_d;
CHECK_CUDA(cudaMalloc(&d_a, sizeof(int)));
CHECK_CUDA(cudaMalloc(&d_b, sizeof(int)));
CHECK_CUDA(cudaMalloc(&d_c, sizeof(int)));
CHECK_CUDA(cudaMalloc(&d_d, sizeof(int)));
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int c = atoi(argv[3]);
2023-02-15 01:33:28 +09:00
int d;
2023-02-14 20:17:53 +09:00
CHECK_CUDA(cudaMemcpy(d_a, &a, sizeof(int), cudaMemcpyHostToDevice));
CHECK_CUDA(cudaMemcpy(d_b, &b, sizeof(int), cudaMemcpyHostToDevice));
CHECK_CUDA(cudaMemcpy(d_c, &c, sizeof(int), cudaMemcpyHostToDevice));
pythagoras<<<1, 1>>>(d_a, d_b, d_c, d_d);
CHECK_CUDA(cudaGetLastError());
CHECK_CUDA(cudaMemcpy(&d, d_d, sizeof(int), cudaMemcpyDeviceToHost));
if (d == 1) {
printf("YES\n");
} else {
printf("NO\n");
}
CHECK_CUDA(cudaFree(d_a));
CHECK_CUDA(cudaFree(d_b));
CHECK_CUDA(cudaFree(d_c));
CHECK_CUDA(cudaFree(d_d));
return 0;
}