大家:最近尝试利用cuda 5.5最新的特性来编程,即动态并行。但我有一些非常令人困惑的问题。我的代码在这里:
/* Includes, system */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
/* Includes, cuda */
#include <cuda_runtime.h>
#include <cublas_v2.h>
/* Includes, cuda helper functions */
#include <helper_cuda.h>
#include "kernels.cu"
/* Matrix size */
#define N (275)
#define LengthSignal (64)
#define AmountSignal (255025)
#define NBLOCKX (32768)
#define NTHREADS_PER_BLOCK (128)
/* Declaration of the function that computes sgemm using CUBLAS device API */
__global__ void invokeDeviceCublasSgemm(float *d_A, float *Test);
/* Main */
int main(int argc, char **argv)
{
float *h_A;
float *d_A = 0;
int n2 = N * N;
h_A = (float *)malloc(n2 * sizeof(h_A[0]));
/* Fill the matrices with test data */
for (int i = 0; i < n2; i++)
{
h_A[i] = rand() / (float)RAND_MAX;
}
cudaMalloc((void **)&d_A, n2 * sizeof(h_A[0]));
/* Initialize the device matrices with the host matrices */
// cudaMemcpy(d_A, h_A, sizeof(float) * LengthSignal * AmountSignal, cudaMemcpyHostToDevice);
cudaMemcpy(d_A, h_A, n2 * sizeof(h_A[0]), cudaMemcpyHostToDevice);
int Length = 100;
float *h_Test = (float *) malloc(sizeof(float) * Length);
float *d_Test;
cudaMalloc((void **) &d_Test, sizeof(float) * Length);
cudaMemset(d_Test, 0, sizeof(float) * Length);
invokeDeviceCublasSgemm<<<NBLOCKX, NTHREADS_PER_BLOCK>>>(d_A, d_Test);
cudaMemcpy(h_Test, d_Test, sizeof(float) * Length, cudaMemcpyDeviceToHost);
printf("\n The first 10 elements of d_A in location 1 are: \n");
for (int j = 0; j < 10; j ++)
{
printf("%f ", h_Test[j]);
}
printf("\n The first 10 elements of d_A in location 2 are: \n");
for (int j = 10; j < 20; j ++)
{
printf("%f ", h_Test[j]);
}
printf("\n");
free(h_Test);
cudaFree(d_Test);
/* Memory clean up */
free(h_A);
cudaFree(d_A);
}
#ifndef __GLOBAL__CU__
#define __GLOBAL__CU__
__global__ void invokeDeviceCublasSgemm(float *d_A, float *Test)
{
// save the first 10 elements of d_A in location 1
for (int j = 0; j < 10; j ++)
{
Test[j] = d_A[j];
}
cublasHandle_t cnpHandle;
cublasCreate(&cnpHandle);
// save the first 10 elements of d_A in location 2
for (int j = 10; j < 20; j ++)
{
Test[j] = d_A[j - 10];
}
cublasDestroy(cnpHandle);
}
#endif
如果我将配置参数设置为 <<<1, 1>>>,一切正常。输出是这样的:
位置 1 中 d_A 的前 10 个元素是:
0.840188 0.394383 0.783099 0.798440 0.911647 0.197551 0.335223 0.768230 0.277775 0.553970
位置 2 中 d_A 的前 10 个元素是:
0.840188 0.394383 0.783099 0.798440 0.911647 0.197551 0.335223 0.768230 0.277775 0.553970
但是,如果我将配置参数设置为 <<<32768, 128>>>,则输出很奇怪。输出是这样的:
位置 1 中 d_A 的前 10 个元素是:
-0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
位置 2 中 d_A 的前 10 个元素是:
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
我真的不知道为什么!我的代码只是来自“样本”,稍作改动。
然后我把最后一个代码“cublasDestroy(cnpHandle);”删掉,就正常了。输出是:
位置 1 中 d_A 的前 10 个元素是:
0.840188 0.394383 0.783099 0.798440 0.911647 0.197551 0.335223 0.768230 0.277775 0.553970
位置 2 中 d_A 的前 10 个元素是:
0.840188 0.394383 0.783099 0.798440 0.911647 0.197551 0.335223 0.768230 0.277775 0.553970
有人有同样的问题吗?
谢谢!