| 177 | /******************** DevSparseMatrix ********************/ |
| 178 | |
| 179 | DevSparseMatrix :: DevSparseMatrix (const SparseMatrix<double> & mat) |
| 180 | { |
| 181 | height = mat.Height(); |
| 182 | width = mat.Width(); |
| 183 | nze = mat.NZE(); |
| 184 | |
| 185 | cout << IM(7) << "DevSparseMatrix" << endl |
| 186 | << " height = " << height << ", width = " << width << ", nze = " << nze << endl; |
| 187 | |
| 188 | Array<int> temp_ind (height+1); |
| 189 | for (int i = 0; i <= height; i++) temp_ind[i] = mat.First(i); // conversion to 32-bit integer |
| 190 | |
| 191 | cudaMalloc ((void**)&dev_ind, (mat.Height()+1) * sizeof(int)); |
| 192 | cudaMalloc ((void**)&dev_col, (mat.NZE()) * sizeof(int)); |
| 193 | cudaMalloc ((void**)&dev_val, (mat.NZE()) * sizeof(double)); |
| 194 | |
| 195 | cudaMemcpy (dev_ind, temp_ind.Data(), (mat.Height()+1)*sizeof(int), cudaMemcpyHostToDevice); |
| 196 | cudaMemcpy (dev_col, mat.GetRowIndices(0).Data(), mat.NZE()*sizeof(int), cudaMemcpyHostToDevice); |
| 197 | cudaMemcpy (dev_val, mat.GetRowValues(0).Data(), mat.NZE()*sizeof(double), cudaMemcpyHostToDevice); |
| 198 | |
| 199 | cusparseCreateCsr(&descr, height, width, nze, |
| 200 | dev_ind, dev_col, dev_val, |
| 201 | CUSPARSE_INDEX_32I, CUSPARSE_INDEX_32I, CUSPARSE_INDEX_BASE_ZERO, |
| 202 | CUDA_R_64F); |
| 203 | |
| 204 | // pre-compute buffer size, preprocess for graph capture |
| 205 | { double alpha=1, beta=0; |
| 206 | // std::cerr << "[preprocess] entering block, handle=" << Get_CuSparse_Handle() << std::endl; |
| 207 | double *raw_x = nullptr, *raw_y = nullptr; |
| 208 | auto err1 = cudaMalloc(&raw_x, width * sizeof(double)); |
| 209 | auto err2 = cudaMalloc(&raw_y, height * sizeof(double)); |
| 210 | // std::cerr << "[preprocess] raw_x=" << raw_x << " err1=" << err1 << " raw_y=" << raw_y << " err2=" << err2 << std::endl; |
| 211 | cusparseDnVecDescr_t dx, dy; |
| 212 | cusparseCreateDnVec(&dx, width, raw_x, CUDA_R_64F); |
| 213 | cusparseCreateDnVec(&dy, height, raw_y, CUDA_R_64F); |
| 214 | // std::cerr << "[preprocess] calling bufferSize" << std::endl; |
| 215 | cusparseSpMV_bufferSize(Get_CuSparse_Handle(), CUSPARSE_OPERATION_NON_TRANSPOSE, |
| 216 | &alpha, descr, dx, &beta, dy, CUDA_R_64F, |
| 217 | CUSPARSE_SPMV_ALG_DEFAULT, &spmv_bufferSize); |
| 218 | // std::cerr << "[preprocess] bufferSize=" << spmv_bufferSize << " calling cudaMalloc" << std::endl; |
| 219 | cudaMalloc(&spmv_buffer, spmv_bufferSize); |
| 220 | // std::cerr << "[preprocess] buffer allocated OK, skipping preprocess" << std::endl; |
| 221 | cusparseDestroyDnVec(dx); cusparseDestroyDnVec(dy); |
| 222 | cudaFree(raw_x); cudaFree(raw_y); } |
| 223 | } |
| 224 | |
| 225 | |
| 226 | DevSparseMatrix :: ~DevSparseMatrix () |