Advertisement
Guest User

Untitled

a guest
Apr 25th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <stdlib.h>
  4. #include <cuda_runtime.h>
  5. #include "device_launch_parameters.h"
  6. #include "windows.h"
  7. #if __DEVICE_EMULATION__
  8. bool InitCUDA(void) {
  9.     fprintf(stderr, "Режим эмуляции\n");
  10.     return true;
  11. }
  12. #else
  13. bool InitCUDA(void) {
  14.     int deviceCount;
  15.     cudaGetDeviceCount(&deviceCount);
  16.     if (deviceCount == 0) {
  17.         printf("Устройства CUDA не обнаружены\n");
  18.         return false;
  19.     }
  20.     for (int dev = 0; dev < deviceCount; dev++) {
  21.         cudaDeviceProp deviceProp;
  22.         cudaGetDeviceProperties(&deviceProp, dev);
  23.         if (dev == 0) {
  24.             if (deviceProp.major == 9999 && deviceProp.minor == 9999) {
  25.                 printf("Устройства CUDA не обнаружены\n");
  26.                 return false;
  27.             }
  28.             else if (deviceCount == 1) {
  29.                 printf("Найдено 1 CUDA устройство\n");
  30.             }
  31.             else {
  32.                 printf("Найдено %dCUDA устройств \n", deviceCount);
  33.             }
  34.         }
  35.     }
  36.     return true;
  37. }
  38. #endif
  39. __global__ void calc(double *a, int n) {
  40.     int idx = blockIdx.x * blockDim.x + threadIdx.x;
  41.     double val = a[idx];
  42.     if (idx < n) {
  43.         a[idx] = 4.0 / (1.0 + val * val);
  44.     }
  45. }
  46. int main(int argc, char* argv[]) {
  47.     setlocale(LC_ALL, "Russian");
  48.     if (!InitCUDA()) {
  49.         system("pause");
  50.         return 0;
  51.     }
  52.     int Time1, Time2, Delay1;
  53.     double *a_h; // указатель на область памяти хоста
  54.     double *a_d; // указатель на область памяти устройства
  55.     const int numSteps = 10000000; // количество разбиений
  56.     a_h = (double *)malloc(sizeof(double)*numSteps); // выделение памяти на хосте обычным способом
  57.     cudaMalloc((void **)&a_d, sizeof(double)*numSteps); // выделение памяти на устройстве
  58.     int blockSize = 192;
  59.     int blocks = numSteps / blockSize + (numSteps % blockSize == 0 ? 0 : 1);
  60.     double left = 0.0;
  61.     double right = 1.0;
  62.     double step = (right - left) / numSteps;
  63.     int i = 0;
  64.     for (double x = left + 0.5 * step; x < right; x += step) {
  65.         a_h[i] = x;
  66.         i++;
  67.     }
  68.     cudaMemcpy(a_d, a_h, sizeof(double)*numSteps, cudaMemcpyHostToDevice);
  69.     Time1 = GetTickCount();
  70.     calc <<< blocks, blockSize >>>(a_d, numSteps);
  71.     cudaMemcpy(a_h, a_d, sizeof(double)*numSteps, cudaMemcpyDeviceToHost); // передаем данные обратно на хост
  72.     double sum = 0.0;
  73.     for (int i = 0; i < numSteps; i++) {
  74.         sum += a_h[i];
  75.     }
  76.     Time2 = GetTickCount();
  77.     Delay1 = Time2 - Time1;
  78.     printf("Значение интеграла %0.7f\n", sum / numSteps);
  79.     printf("\nВремя вычисления = %d ms\n", Delay1);
  80.     system("pause");
  81.     free(a_h);
  82.     cudaFree(a_d);
  83.     return EXIT_SUCCESS;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement