Advertisement
desdemona

cuda

Apr 14th, 2015
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.79 KB | None | 0 0
  1. /*
  2.  * Copyright 1993-2013 NVIDIA Corporation.  All rights reserved.
  3.  *
  4.  * Please refer to the NVIDIA end user license agreement (EULA) associated
  5.  * with this source code for terms and conditions that govern your use of
  6.  * this software. Any use, reproduction, disclosure, or distribution of
  7.  * this software and related documentation outside the terms of the EULA
  8.  * is strictly prohibited.
  9.  *
  10.  */
  11.  
  12. /*
  13.  * This sample implements a conjugate graident solver on GPU
  14.  * using CUBLAS and CUSPARSE
  15.  *
  16.  */
  17.  
  18. // includes, system
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. /* Using updated (v2) interfaces to cublas and cusparse */
  24. #include <cuda_runtime.h>
  25. #include <cusparse_v2.h>
  26. #include <cublas_v2.h>
  27.  
  28. // Utilities and system includes
  29. #include <helper_functions.h>  // helper for shared functions common to CUDA SDK samples
  30. #include <helper_cuda.h>       // helper function CUDA error checking and intialization
  31.  
  32. const char *sSDKname     = "conjugateGradient";
  33.  
  34. double mclock(){
  35.      struct timeval tp;
  36.  
  37.      double sec,usec;
  38.      gettimeofday( &tp, NULL );
  39.      sec    = double( tp.tv_sec );
  40.      usec   = double( tp.tv_usec )/1E6;
  41.      return sec + usec;
  42. }
  43.  
  44.  
  45. #define dot_BS     32
  46. #define kernel_BS  32
  47.  
  48. /* genTridiag: generate a random tridiagonal symmetric matrix */
  49. void genTridiag(int *I, int *J, float *val, int N, int nz)
  50. {
  51.     double RAND_MAXi = 1e6;
  52.     double val_r     = 12.345 * 1e5;
  53.    
  54.     I[0] = 0, J[0] = 0, J[1] = 1;
  55.     val[0] = (float)val_r/RAND_MAXi + 10.0f;
  56.     val[1] = (float)val_r/RAND_MAXi;
  57.     int start;
  58.  
  59.     for (int i = 1; i < N; i++)
  60.     {
  61.         if (i > 1)
  62.         {
  63.             I[i] = I[i-1]+3;
  64.         }
  65.         else
  66.         {
  67.             I[1] = 2;
  68.         }
  69.  
  70.         start = (i-1)*3 + 2;
  71.         J[start] = i - 1;
  72.         J[start+1] = i;
  73.  
  74.         if (i < N-1)
  75.         {
  76.             J[start+2] = i + 1;
  77.         }
  78.  
  79.         val[start] = val[start-1];
  80.         val[start+1] = (float)val_r/RAND_MAXi + 10.0f;
  81.  
  82.         if (i < N-1)
  83.         {
  84.             val[start+2] = (float)val_r/RAND_MAXi;
  85.         }
  86.     }
  87.  
  88.     I[N] = nz;
  89. }
  90.  
  91.  
  92. void cgs_basic(int argc, char **argv, int N, int M){
  93.  
  94.     //int M = 0, N = 0,
  95.     int nz = 0, *I = NULL, *J = NULL;
  96.     float *val = NULL;
  97.     const float tol = 1e-10f;
  98.     const int max_iter = 1000;
  99.     float *x;
  100.     float *rhs;
  101.     float a, b, na, r0, r1;
  102.     int *d_col, *d_row;
  103.     float *d_val, *d_x, dot;
  104.     float *d_r, *d_p, *d_Ax;
  105.     int k;
  106.     float alpha, beta, alpham1;
  107.  
  108.     // This will pick the best possible CUDA capable device
  109.     cudaDeviceProp deviceProp;
  110.     int devID = findCudaDevice(argc, (const char **)argv);
  111.  
  112.     if (devID < 0)
  113.     {
  114.         printf("exiting...\n");
  115.         exit(EXIT_SUCCESS);
  116.     }
  117.  
  118.     checkCudaErrors(cudaGetDeviceProperties(&deviceProp, devID));
  119.  
  120.     // Statistics about the GPU device
  121.     printf("> GPU device has %d Multi-Processors, SM %d.%d compute capabilities\n\n",
  122.            deviceProp.multiProcessorCount, deviceProp.major, deviceProp.minor);
  123.  
  124.     int version = (deviceProp.major * 0x10 + deviceProp.minor);
  125.  
  126.     if (version < 0x11)
  127.     {
  128.         printf("%s: requires a minimum CUDA compute 1.1 capability\n", sSDKname);
  129.         cudaDeviceReset();
  130.         exit(EXIT_SUCCESS);
  131.     }
  132.  
  133.     /* Generate a random tridiagonal symmetric matrix in CSR format */
  134.     //M = N = 32*64;//10; //1048576;
  135.     printf("M = %d, N = %d\n", M, N);
  136.     nz = (N-2)*3 + 4;
  137.     I = (int *)malloc(sizeof(int)*(N+1));
  138.     J = (int *)malloc(sizeof(int)*nz);
  139.     val = (float *)malloc(sizeof(float)*nz);
  140.     genTridiag(I, J, val, N, nz);
  141.  
  142.     /*
  143.     for (int i = 0; i < nz; i++){
  144.         printf("%d\t", J[i]);
  145.     }
  146.     printf("\n");
  147.     for (int i = 0; i < nz; i++){
  148.         printf("%2f\t", val[i]);
  149.     }
  150.     */
  151.  
  152.     x = (float *)malloc(sizeof(float)*N);
  153.     rhs = (float *)malloc(sizeof(float)*N);
  154.  
  155.     for (int i = 0; i < N; i++)
  156.     {
  157.         rhs[i] = 1.0;
  158.         x[i] = 0.0;
  159.     }
  160.  
  161.     /* Get handle to the CUBLAS context */
  162.     cublasHandle_t cublasHandle = 0;
  163.     cublasStatus_t cublasStatus;
  164.     cublasStatus = cublasCreate(&cublasHandle);
  165.  
  166.     checkCudaErrors(cublasStatus);
  167.  
  168.     /* Get handle to the CUSPARSE context */
  169.     cusparseHandle_t cusparseHandle = 0;
  170.     cusparseStatus_t cusparseStatus;
  171.     cusparseStatus = cusparseCreate(&cusparseHandle);
  172.  
  173.     checkCudaErrors(cusparseStatus);
  174.  
  175.     cusparseMatDescr_t descr = 0;
  176.     cusparseStatus = cusparseCreateMatDescr(&descr);
  177.  
  178.     checkCudaErrors(cusparseStatus);
  179.  
  180.     cusparseSetMatType(descr,CUSPARSE_MATRIX_TYPE_GENERAL);
  181.     cusparseSetMatIndexBase(descr,CUSPARSE_INDEX_BASE_ZERO);
  182.  
  183.     checkCudaErrors(cudaMalloc((void **)&d_col, nz*sizeof(int)));
  184.     checkCudaErrors(cudaMalloc((void **)&d_row, (N+1)*sizeof(int)));
  185.     checkCudaErrors(cudaMalloc((void **)&d_val, nz*sizeof(float)));
  186.     checkCudaErrors(cudaMalloc((void **)&d_x, N*sizeof(float)));
  187.     checkCudaErrors(cudaMalloc((void **)&d_r, N*sizeof(float)));
  188.     checkCudaErrors(cudaMalloc((void **)&d_p, N*sizeof(float)));
  189.     checkCudaErrors(cudaMalloc((void **)&d_Ax, N*sizeof(float)));
  190.  
  191.     cudaMemcpy(d_col, J, nz*sizeof(int), cudaMemcpyHostToDevice);
  192.     cudaMemcpy(d_row, I, (N+1)*sizeof(int), cudaMemcpyHostToDevice);
  193.     cudaMemcpy(d_val, val, nz*sizeof(float), cudaMemcpyHostToDevice);
  194.     cudaMemcpy(d_x, x, N*sizeof(float), cudaMemcpyHostToDevice);
  195.     cudaMemcpy(d_r, rhs, N*sizeof(float), cudaMemcpyHostToDevice);
  196.  
  197.     alpha = 1.0;
  198.     alpham1 = -1.0;
  199.     beta = 0.0;
  200.     r0 = 0.;
  201.  
  202.  
  203.     double t_start = mclock();
  204.     cusparseScsrmv(cusparseHandle,CUSPARSE_OPERATION_NON_TRANSPOSE, N, N, nz, &alpha, descr, d_val, d_row, d_col, d_x, &beta, d_Ax);
  205.  
  206.     cublasSaxpy(cublasHandle, N, &alpham1, d_Ax, 1, d_r, 1);                                // PODMIEN FUNCKJE (I)
  207.     cublasStatus = cublasSdot(cublasHandle, N, d_r, 1, d_r, 1, &r1);                        // PODMIEN FUNCKJE (II)
  208.  
  209.     k = 1;
  210.  
  211.     while (r1 > tol*tol && k <= max_iter)
  212.     {
  213.         if (k > 1)
  214.         {
  215.             b = r1 / r0;
  216.             cublasStatus = cublasSscal(cublasHandle, N, &b, d_p, 1);                        // PODMIEN FUNCKJE (I)
  217.             cublasStatus = cublasSaxpy(cublasHandle, N, &alpha, d_r, 1, d_p, 1);            // PODMIEN FUNCKJE (I)
  218.         }
  219.         else
  220.         {
  221.             cublasStatus = cublasScopy(cublasHandle, N, d_r, 1, d_p, 1);                    // PODMIEN FUNCKJE (I)
  222.         }
  223.  
  224.         cusparseScsrmv(cusparseHandle, CUSPARSE_OPERATION_NON_TRANSPOSE, N, N, nz, &alpha, descr, d_val, d_row, d_col, d_p, &beta, d_Ax); // PODMIEN FUNCKJE (III)
  225.         cublasStatus = cublasSdot(cublasHandle, N, d_p, 1, d_Ax, 1, &dot);                  // PODMIEN FUNCKJE (II)
  226.         a = r1 / dot;
  227.  
  228.         cublasStatus = cublasSaxpy(cublasHandle, N, &a, d_p, 1, d_x, 1);                    // PODMIEN FUNCKJE (I)
  229.         na = -a;
  230.         cublasStatus = cublasSaxpy(cublasHandle, N, &na, d_Ax, 1, d_r, 1);                  // PODMIEN FUNCKJE (I)
  231.  
  232.         r0 = r1;
  233.         cublasStatus = cublasSdot(cublasHandle, N, d_r, 1, d_r, 1, &r1);                    // PODMIEN FUNCKJE (II)
  234.         cudaThreadSynchronize();
  235.         printf("iteration = %3d, residual = %e\n", k, sqrt(r1));
  236.         k++;
  237.     }
  238.     printf("TIME OF CGS_BASIC = %f\n", mclock() - t_start);
  239.  
  240.     cudaMemcpy(x, d_x, N*sizeof(float), cudaMemcpyDeviceToHost);
  241.  
  242.     float rsum, diff, err = 0.0;
  243.  
  244.     for (int i = 0; i < N; i++)
  245.     {
  246.         rsum = 0.0;
  247.  
  248.         for (int j = I[i]; j < I[i+1]; j++)
  249.         {
  250.             rsum += val[j]*x[J[j]];
  251.         }
  252.  
  253.         diff = fabs(rsum - rhs[i]);
  254.  
  255.         if (diff > err)
  256.         {
  257.             err = diff;
  258.         }
  259.     }
  260.  
  261.     cusparseDestroy(cusparseHandle);
  262.     cublasDestroy(cublasHandle);
  263.  
  264.     free(I);
  265.     free(J);
  266.     free(val);
  267.     free(x);
  268.     free(rhs);
  269.     cudaFree(d_col);
  270.     cudaFree(d_row);
  271.     cudaFree(d_val);
  272.     cudaFree(d_x);
  273.     cudaFree(d_r);
  274.     cudaFree(d_p);
  275.     cudaFree(d_Ax);
  276.  
  277.     cudaDeviceReset();
  278.  
  279.     printf("Test Summary:  Error amount = %e\n", err);
  280.     //exit((k <= max_iter) ? 0 : 1);
  281.  
  282.  
  283. }
  284.  
  285. __global__
  286. void mycpy(float *a, float *b, int N)
  287. {
  288.   int num = blockDim.x * blockIdx.x + threadIdx.x;
  289.  
  290.   if(num < N)
  291.   {a[num] = b[num];}
  292. }
  293.  
  294.  
  295. __global__
  296. void myscal( float *y, float b, int N)
  297. {
  298.     int num = blockDim.x * blockIdx.x + threadIdx.x;
  299.   if(num < N)
  300.   {y[num] = y[num]*b;}
  301. }
  302.  
  303. __global__
  304. void myaxpy(float *y, float *x,float a, int N)
  305. {
  306.     int num = blockDim.x * blockIdx.x + threadIdx.x;
  307.   if(num < N)
  308.   {
  309.     y[num] = y[num] + a*x[num];
  310.   }
  311. }
  312.  
  313. /*
  314. __global__
  315. void myMatVec(float **y,float *x, int N)
  316. {
  317.   int num=blockDim.x * blockIdx.x + threadIdx.x;
  318.   if(num < N)
  319.   {
  320.     int i=0;
  321.     for(i=0;i<N;i++)
  322.     {
  323.       float sub = 0.0;
  324.       int j =0;
  325.       for(j=x[i]; j<x[i+1];j++)
  326.       {
  327.     sub += values[j] * x[colubd[j]];
  328.       }
  329.     }
  330.   }
  331. }*/
  332.  
  333.  
  334. void cgs_TODO(int argc, char **argv, int N, int M){
  335.  
  336.     //int M = 0, N = 0,
  337.     int nz = 0, *I = NULL, *J = NULL;
  338.     float *val = NULL;
  339.     const float tol = 1e-10f;
  340.     const int max_iter = 1000;
  341.     float *x;
  342.     float *rhs;
  343.     float a, b, na, r0, r1;
  344.     int *d_col, *d_row;
  345.     float *d_val, *d_x, dot;
  346.     float *d_r, *d_p, *d_Ax;
  347.     int k;
  348.     float alpha, beta, alpham1;
  349.  
  350.     // This will pick the best possible CUDA capable device
  351.     cudaDeviceProp deviceProp;
  352.     int devID = findCudaDevice(argc, (const char **)argv);
  353.  
  354.     if (devID < 0)
  355.     {
  356.         printf("exiting...\n");
  357.         exit(EXIT_SUCCESS);
  358.     }
  359.  
  360.     checkCudaErrors(cudaGetDeviceProperties(&deviceProp, devID));
  361.  
  362.     // Statistics about the GPU device
  363.     printf("> GPU device has %d Multi-Processors, SM %d.%d compute capabilities\n\n",
  364.            deviceProp.multiProcessorCount, deviceProp.major, deviceProp.minor);
  365.  
  366.     int version = (deviceProp.major * 0x10 + deviceProp.minor);
  367.  
  368.     if (version < 0x11)
  369.     {
  370.         printf("%s: requires a minimum CUDA compute 1.1 capability\n", sSDKname);
  371.         cudaDeviceReset();
  372.         exit(EXIT_SUCCESS);
  373.     }
  374.  
  375.     /* Generate a random tridiagonal symmetric matrix in CSR format */
  376.     //M = N = 32*64;//10; //1048576;
  377.     printf("M = %d, N = %d\n", M, N);
  378.     nz = (N-2)*3 + 4;
  379.     I = (int *)malloc(sizeof(int)*(N+1));
  380.     J = (int *)malloc(sizeof(int)*nz);
  381.     val = (float *)malloc(sizeof(float)*nz);
  382.     genTridiag(I, J, val, N, nz);
  383.  
  384.     /*
  385.     for (int i = 0; i < nz; i++){
  386.         printf("%d\t", J[i]);
  387.     }
  388.     printf("\n");
  389.     for (int i = 0; i < nz; i++){
  390.         printf("%2f\t", val[i]);
  391.     }
  392.     */
  393.  
  394.     x = (float *)malloc(sizeof(float)*N);
  395.     rhs = (float *)malloc(sizeof(float)*N);
  396.  
  397.     for (int i = 0; i < N; i++)
  398.     {
  399.         rhs[i] = 1.0;
  400.         x[i] = 0.0;
  401.     }
  402.  
  403.     /* Get handle to the CUBLAS context */
  404.     cublasHandle_t cublasHandle = 0;
  405.     cublasStatus_t cublasStatus;
  406.     cublasStatus = cublasCreate(&cublasHandle);
  407.  
  408.     checkCudaErrors(cublasStatus);
  409.  
  410.     /* Get handle to the CUSPARSE context */
  411.     cusparseHandle_t cusparseHandle = 0;
  412.     cusparseStatus_t cusparseStatus;
  413.     cusparseStatus = cusparseCreate(&cusparseHandle);
  414.  
  415.     checkCudaErrors(cusparseStatus);
  416.  
  417.     cusparseMatDescr_t descr = 0;
  418.     cusparseStatus = cusparseCreateMatDescr(&descr);
  419.  
  420.     checkCudaErrors(cusparseStatus);
  421.  
  422.     cusparseSetMatType(descr,CUSPARSE_MATRIX_TYPE_GENERAL);
  423.     cusparseSetMatIndexBase(descr,CUSPARSE_INDEX_BASE_ZERO);
  424.  
  425.     checkCudaErrors(cudaMalloc((void **)&d_col, nz*sizeof(int)));
  426.     checkCudaErrors(cudaMalloc((void **)&d_row, (N+1)*sizeof(int)));
  427.     checkCudaErrors(cudaMalloc((void **)&d_val, nz*sizeof(float)));
  428.     checkCudaErrors(cudaMalloc((void **)&d_x, N*sizeof(float)));
  429.     checkCudaErrors(cudaMalloc((void **)&d_r, N*sizeof(float)));
  430.     checkCudaErrors(cudaMalloc((void **)&d_p, N*sizeof(float)));
  431.     checkCudaErrors(cudaMalloc((void **)&d_Ax, N*sizeof(float)));
  432.  
  433.     cudaMemcpy(d_col, J, nz*sizeof(int), cudaMemcpyHostToDevice);
  434.     cudaMemcpy(d_row, I, (N+1)*sizeof(int), cudaMemcpyHostToDevice);
  435.     cudaMemcpy(d_val, val, nz*sizeof(float), cudaMemcpyHostToDevice);
  436.     cudaMemcpy(d_x, x, N*sizeof(float), cudaMemcpyHostToDevice);
  437.     cudaMemcpy(d_r, rhs, N*sizeof(float), cudaMemcpyHostToDevice);
  438.  
  439.     alpha = 1.0;
  440.     alpham1 = -1.0;
  441.     beta = 0.0;
  442.     r0 = 0.;
  443.  
  444.  
  445.     int block_size = 4;
  446.     int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
  447.    
  448.     // sparse matrix vector product: d_Ax = A * d_x
  449.     cusparseScsrmv(cusparseHandle,CUSPARSE_OPERATION_NON_TRANSPOSE, N, N, nz, &alpha, descr, d_val, d_row, d_col, d_x, &beta, d_Ax);  // PODMIEN FUNCKJE (ZADANIE-I)
  450.  
  451.  
  452.     //azpy: d_r = d_r + alpham1 * d_Ax
  453.     cublasSaxpy(cublasHandle, N, &alpham1, d_Ax, 1, d_r, 1);                        // PODMIEN FUNCKJE (ZADANIE-I)
  454.     //dot:  r1 = d_r * d_r
  455.     cublasStatus = cublasSdot(cublasHandle, N, d_r, 1, d_r, 1, &r1);                        // PODMIEN FUNCKJE (ZADANIE-III)
  456.  
  457.     k = 1;
  458.  
  459.     while (r1 > tol*tol && k <= max_iter)
  460.     {
  461.         if (k > 1)
  462.         {
  463.             b = r1 / r0;
  464.         //scal: d_p = b * d_p
  465.             //cublasStatus = cublasSscal(cublasHandle, N, &b, d_p, 1);                        // PODMIEN FUNCKJE (ZADANIE-I)
  466.         myscal <<< n_blocks,block_size>>> (d_p,b,N);
  467.         //axpy:  d_p = d_p + alpha * d_r
  468.             //cublasStatus = cublasSaxpy(cublasHandle, N, &alpha, d_r, 1, d_p, 1);            // PODMIEN FUNCKJE (ZADANIE-I)
  469.         myaxpy <<< n_blocks,block_size>>> (d_p,d_r,alpha,N);
  470.      
  471.     }
  472.         else
  473.         {
  474.             //cpy: d_p = d_r
  475.             //cublasStatus = cublasScopy(cublasHandle, N, d_r, 1, d_p, 1);                   // PODMIEN FUNCKJE (ZADANIE-I)
  476.         //mycpy(d_p,d_r,N);
  477.         mycpy <<< n_blocks, block_size >>> (d_p, d_r, N);
  478.         }
  479.  
  480.         //sparse matrix-vector product: d_Ax = A * d_p
  481.         cusparseScsrmv(cusparseHandle, CUSPARSE_OPERATION_NON_TRANSPOSE, N, N, nz, &alpha, descr, d_val, d_row, d_col, d_p, &beta, d_Ax); // PODMIEN FUNCKJE (ZADANIE-II)
  482.         cublasStatus = cublasSdot(cublasHandle, N, d_p, 1, d_Ax, 1, &dot);                  // PODMIEN FUNCKJE (ZADANIE-III)
  483.         a = r1 / dot;
  484.  
  485.         //axpy: d_x = d_x + a*d_p
  486.         //cublasStatus = cublasSaxpy(cublasHandle, N, &a, d_p, 1, d_x, 1);                    // PODMIEN FUNCKJE (ZADANIE-I)
  487.     myaxpy <<< n_blocks,block_size>>> (d_x,d_p,a,N);
  488.         na = -a;
  489.      
  490.         //axpy:  d_r = d_r + na * d_Ax
  491.         //cublasStatus = cublasSaxpy(cublasHandle, N, &na, d_Ax, 1, d_r, 1);                  // PODMIEN FUNCKJE (ZADANIE-I)
  492.     myaxpy <<< n_blocks,block_size>>> (d_r,d_Ax,na,N);
  493.         r0 = r1;
  494.    
  495.         //dot: r1 = d_r * d_r
  496.         cublasStatus = cublasSdot(cublasHandle, N, d_r, 1, d_r, 1, &r1);                    // PODMIEN FUNCKJE (ZADANIE-III)
  497.         cudaThreadSynchronize();
  498.         printf("iteration = %3d, residual = %e\n", k, sqrt(r1));
  499.         k++;
  500.     }
  501.  
  502.     cudaMemcpy(x, d_x, N*sizeof(float), cudaMemcpyDeviceToHost);
  503.  
  504.     float rsum, diff, err = 0.0;
  505.  
  506.     for (int i = 0; i < N; i++)
  507.     {
  508.         rsum = 0.0;
  509.  
  510.         for (int j = I[i]; j < I[i+1]; j++)
  511.         {
  512.             rsum += val[j]*x[J[j]];
  513.         }
  514.  
  515.         diff = fabs(rsum - rhs[i]);
  516.  
  517.         if (diff > err)
  518.         {
  519.             err = diff;
  520.         }
  521.     }
  522.  
  523.     cusparseDestroy(cusparseHandle);
  524.     cublasDestroy(cublasHandle);
  525.  
  526.     free(I);
  527.     free(J);
  528.     free(val);
  529.     free(x);
  530.     free(rhs);
  531.     cudaFree(d_col);
  532.     cudaFree(d_row);
  533.     cudaFree(d_val);
  534.     cudaFree(d_x);
  535.     cudaFree(d_r);
  536.     cudaFree(d_p);
  537.     cudaFree(d_Ax);
  538.  
  539.     cudaDeviceReset();
  540.  
  541.     printf("Test Summary:  Error amount = %e\n", err);
  542.     //exit((k <= max_iter) ? 0 : 1);
  543.  
  544.  
  545. }
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553. int main(int argc, char **argv)
  554. {
  555.     //int N = 1e6;//1 << 20;
  556.     //int N = 256 * (1<<10)  -10 ; //1e6;//1 << 20;
  557.     int N = 1e5;
  558.     int M = N;
  559.    
  560.     cgs_basic(argc, argv, N, M);
  561.    
  562.     cgs_TODO(argc, argv, N, M);
  563. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement