Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. void SmithWatermanGPU(std::string const& s1, std::string const& s2, double const d, double const e, double const B)
  2. {
  3.     //DATA PREPARATION
  4.  
  5.  
  6.     //input strings are const so we copy
  7.     std::string string_m(s1);
  8.     std::string string_n(s2);
  9.  
  10.     //memory locations
  11.     float *Gi,*Gd,*F,*E;
  12.     float *memory;
  13.     char *M;
  14.  
  15.     //sizes of strings
  16.     long int m = string_m.length();
  17.     long int n = string_n.length();
  18.  
  19.  
  20.     //B is the desirable number of blocks in grid
  21.     double k = sqrt(B/(m/n));
  22.     long int blockSize_n = floor(k);
  23.     long int blockSize_m = floor((m/n)*k);
  24.     long int blockSize = blockSize_n*blockSize_m;
  25.  
  26.     //std::cout<<k<<" "<<blockSize_n<<" "<<blockSize_m<<std::endl;
  27.     //here we define how much will there be blocks in m and n direction
  28.     long int blockNum_n = ceil((double)n/blockSize_n);
  29.     long int blockNum_m = ceil((double)m/blockSize_m); 
  30.     long int blockNum = blockNum_m*blockNum_n;
  31.  
  32.     //std::cout<<"Size:"<<n<<" "<<blockSize_n<<" "<<ceil((double)n/blockSize_n)<<" "<<ceil(n/blockSize_n)<<std::endl;
  33.     //std::cout<<"Size:"<<m<<" "<<blockSize_m<<" "<<ceil((double)m/blockSize_m)<<" "<<ceil(m/blockSize_m)<<std::endl;
  34.     //here we are padding strings so there are no elements that will be
  35.          
  36.     padding(string_m,string_n,blockNum_m*blockSize_m,blockNum_n*blockSize_n);
  37.     //std::cout<<string_m<<std::endl;
  38.     //std::cout<<string_n<<std::endl;
  39.     //std::cout<<"Size:"<<string_m.length()<<" "<<string_n.length()<<std::endl;
  40.    
  41.     //strings have been padded so their length is measured again   
  42.     m=string_m.length();
  43.     n=string_n.length();   
  44.  
  45.     long int N = (m+1)*(n+1);
  46.     //part of code where memory allocation is happening
  47.     cudaMallocManaged(&memory, N*sizeof(float));
  48.     cudaMallocManaged(&M, N*sizeof(char));
  49.     cudaMallocManaged(&Gi, N*sizeof(float));
  50.     cudaMallocManaged(&Gd, N*sizeof(float));
  51.     cudaMallocManaged(&F, N*sizeof(float));
  52.     cudaMallocManaged(&E, N*sizeof(float));
  53.    
  54.     char* x1 ;//= allocateMemory(string_m);
  55.    
  56.     const char *cstr = string_m.c_str();
  57.         cudaMallocManaged(&x1, string_m.length()*(sizeof(char)+1));
  58.         //x.copy( memory, x.length() );
  59.         //for(int i=0;i<x.length();++i) memory[i]=cstr[i];
  60.         strcpy(x1, cstr);  
  61.         x1[string_m.length()]='\0';
  62.    
  63.         char* x2 ;// = allocateMemory(string_n);
  64.    
  65.     const char *cstr2 = string_n.c_str();
  66.     cudaMallocManaged(&x2, string_n.length()*(sizeof(char)+1));
  67.         //x.copy( memory, x.length() );
  68.         //for(int i=0;i<x.length();++i) memory[i]=cstr[i];
  69.         strcpy(x2, cstr2);  
  70.         x2[string_n.length()]='\0';
  71.    
  72.     int *semaphore;
  73.     //blockSize = 64;
  74.     //std::cout<<blockNum<<" "<<blockSize<<std::endl;
  75.     initsemaphor<<<1, 64>>>(semaphore, blockSize);
  76.     cudaDeviceSynchronize();
  77.  
  78.  
  79.     //CALCULATION
  80.  
  81.     std::cout<<blockNum<<" "<<blockSize<<std::endl;
  82.     //blockSize_m,blockSize_n,blockNum_m,blockNum_n
  83.     //threadSolver<<<1, 64>>>(memory,0,0,n,d,e,5,x1,x2,semaphore);
  84.     //helloWorld<<<1,1>>>();   
  85.    
  86.     //threadSolver(float *memory,long int subM,long int subN, long int const n, float const d, float const e, long int const b_size, char *s1, char *s2, int *semaphore);
  87.     /*for(int i=0;i<N;i++)
  88.     {
  89.         std::cout<<memory[i]<<" ";
  90.     }*/
  91.  
  92.     //memory freeing
  93.     cudaFree(memory);
  94.     cudaFree(M);
  95.     cudaFree(Gi);
  96.     cudaFree(Gd);
  97.     cudaFree(F);
  98.     cudaFree(E);
  99.     cudaFree(semaphore);
  100.  
  101.     return;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement