Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. const int ARRAY_SIZE = 2;
  6. //nvcc -o partone partone.cu
  7.  
  8. __global__ void matMult(double** d_out, double** d_in){
  9. int idx = threadIdx.x;
  10. /*
  11. for(int i = 0; i < ARRAY_SIZE; i++){
  12. d_out[idx][i] = 0;
  13. for(int j = 0; j < ARRAY_SIZE; j++){
  14. d_out[idx][i] += d_in[j][idx] * d_in[j][i];
  15. }
  16. }*/
  17. d_out[0][0] = 6.0;
  18. }
  19.  
  20. int main (int argc, char** argv) {
  21. const int ARRAY_BYTES = ARRAY_SIZE * ARRAY_SIZE * sizeof(double);
  22. //const double min = 1.0;
  23. //const double max = 2.0;
  24.  
  25. double h_in[ARRAY_SIZE][ARRAY_SIZE];
  26. double h_out[ARRAY_SIZE][ARRAY_SIZE];
  27.  
  28. double** d_in;
  29. double** d_out;
  30. //https://ubuntuforums.org/showthread.php?t=1717717&p=10618266#post10618266
  31. //double range = max - min;
  32. //double division = RAND_MAX / range;
  33. for (int i = 0; i < ARRAY_SIZE; i++){
  34. for(int j = 0; j < ARRAY_SIZE; j++){
  35. h_in[i][j] = i + j;
  36. }
  37. }
  38.  
  39. cudaMalloc((void***) &d_in, ARRAY_BYTES);
  40. cudaMalloc((void***) &d_out, ARRAY_BYTES);
  41.  
  42. //transfer the array to the GPU
  43. //dest, source, bytes, direction
  44. cudaMemcpy(d_in, h_in, ARRAY_BYTES, cudaMemcpyHostToDevice);
  45.  
  46. //launch operator: on 1 block of 64 elements
  47. matMult <<< 1, ARRAY_SIZE >>> (d_out, d_in);
  48.  
  49. cudaMemcpy(h_out, d_out, ARRAY_BYTES, cudaMemcpyDeviceToHost);
  50.  
  51. printf("result (0,0): %lf ", h_out[0][0]);
  52. printf("result (0,1): %lf\n", h_out[0][1]);
  53. printf("result (1,0): %lf ", h_out[1][0]);
  54. printf("result (1,1): %lf\n", h_out[1][1]);
  55.  
  56. cudaFree(d_in);
  57. cudaFree(d_out);
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement