1 Linux上C++与CUDA混合编程
完整代码及运算结果
/*
mod_test.cu
*/
#include “cuda_runtime.h”
#include “cublas_v2.h”
#include “device_launch_parameters.h”
#include
#include <stdio.h>
using namespace std;
global void mod_kernel(double* d_M1, double* d_M2, long q, int n) {
// index
int idx = blockIdx.x * blockDim.x + threadIdx.x;
long x;
if (idx < n){
x = d_M1[idx];
// printf(“x: %d\n”, x)
d_M2[idx] = x % q;
}
}
long** mat_mod_cu(long** M1, long rowSize, long colSize, long q, cublasHandle_t cuHandle) {
// 1.定义结果矩阵,用于返回
long** M2 = new long* [rowSize];
for (long i = 0; i < rowSize; i++)
M2[i] = new long[colSize];
// 2.分配CPU资源
double* h_M1 = (double*)malloc(rowSize * colSize * sizeof(double));
double* h_M2 = (double*)malloc