最初我问的是,由于某些原因,当我指定不同的流时,我的内核拒绝同时运行。现在这已经解决了,但是他们的并发行为对我来说仍然不明确。
我知道我的系统可以运行多个流,因为concurrentKernel CUDA示例运行正常。我也可以扩展这个例子,以便模仿我的代码并且它仍然可以同时运行。提前为许多代码道歉。 我想把它全部发布,因为可能有一件小东西会阻止我的内核同时运行,或者我认为它可能与构造或许多单独的文件有关。此外,我确信在试图帮助我时,对你们所有人都有用!我刚刚编写了以下简化程序来复制我的问题:
testMain.c
#include <stdlib.h>
#include <signal.h>
#include "test.h"
#define Nsim 900000
#define Ncomp 20
Vector* test1;
Vector* test2;
Vector* test3;
cudaStream_t stream1;
cudaStream_t stream2;
cudaStream_t stream3;
int
main (int argc, char **argv)
{
test1 = Get_Vector(Nsim);
test2 = Get_Vector(Nsim);
test3 = Get_Vector(Nsim);
checkGPU( cudaStreamCreate(&stream1) );
checkGPU( cudaStreamCreate(&stream2) );
checkGPU( cudaStreamCreate(&stream3) );
int x = 0;
for (x = 0; x < Ncomp; x++)
{
computeGPU(test1, test2, test3, x);
checkGPU( cudaThreadSynchronize() );
}
checkGPU( cudaThreadSynchronize() );
checkGPU( cudaStreamDestroy(stream1) );
checkGPU( cudaStreamDestroy(stream2) );
checkGPU( cudaStreamDestroy(stream3) );
Free_Vector(test1);
Free_Vector(test2);
Free_Vector(test3);
checkGPU( cudaDeviceReset() );
exit(EXIT_SUCCESS);
}
basics.c
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include "basics.h"
inline void gpuAssert(cudaError_t code, const char *file, int line)
{
if (code != cudaSuccess)
{
fprintf(stderr,"CUDA error: %s %s %d\n", cudaGetErrorString(code), file, line);
exit(EXIT_FAILURE);
}
}
basics.h
#ifndef _BASICS_H
#define _BASICS_H
#include <cuda_runtime.h>
#define checkGPU(ans) { gpuAssert((ans), __FILE__, __LINE__); }
void gpuAssert(cudaError_t code, const char *file, int line);
#endif // _BASICS_H
test.cu
extern "C"
{
#include "test.h"
}
__global__ void compute(int* in, int x)
{
int i = blockIdx.x*blockDim.x + threadIdx.x;
in[i] = (int) (x * + 1.05 / 0.4);
}
extern "C" void
computeGPU(Vector* in1, Vector* in2, Vector* in3, int x)
{
int threadsPerBlock = 256;
int blocksPerGrid = (in1->N + threadsPerBlock - 1) / threadsPerBlock;
compute<<<blocksPerGrid, threadsPerBlock, 0, stream1>>>(in1->d_data, x);
compute<<<blocksPerGrid, threadsPerBlock, 0, stream2>>>(in2->d_data, x);
compute<<<blocksPerGrid, threadsPerBlock, 0, stream3>>>(in3->d_data, x);
}
test.h
#ifndef _TEST_H
#define _TEST_H
#include "vector.h"
#include "basics.h"
#include <cuda_runtime.h>
extern cudaStream_t stream1;
extern cudaStream_t stream2;
extern cudaStream_t stream3;
extern void computeGPU(Vector* in1, Vector* in2, Vector* in3, int x);
#endif // _TEST_H
vector.c
#include <stdlib.h>
#include "vector.h"
#include "basics.h"
Vector*
Get_Vector(int N)
{
Vector* v = (Vector*) calloc(1, sizeof(Vector));
v->N = N;
checkGPU( cudaMalloc((void**) &v->d_data, N * sizeof(int)) );
return v;
}
void
Free_Vector(Vector* in)
{
checkGPU( cudaFree(in->d_data) );
free(in);
}
vector.h
#ifndef _VECTOR_H
#define _VECTOR_H
typedef struct
{
int N;
int* d_data;
} Vector;
extern Vector* Get_Vector(int N);
extern void Free_Vector(Vector* in);
#endif // _VECTOR_H
我编译:
nvcc -gencode arch=compute_20,code=sm_20 -O3 -use_fast_math -lineinfo -o test testMain.c test.cu basics.c vector.c; time ./test
并获得在nvvp中运行的单独内核:
在Roberts的帮助下,我通过减少Nsim解决了这个问题。
如果Nsim很小(900),理论上内核可以同时运行,但是我的内核非常简单,它们比启动下一个内核的开销更快,因此整个模拟只是启动计算(int *) RuntimeAPI行中的,int,int)。配置文件结果如下所示
如果我对内核和代码进行更改,以便内核运行时间更长(并将Nsim设置为合理的,3000,现在不重要):
test.cu
__global__ void compute(int* in, int x, int y)
{
int i = blockIdx.x*blockDim.x + threadIdx.x;
in[i] = (int) (x * + 1.05 / 0.4);
int clock_count = 5000000 * y;
clock_t start_clock = clock();
clock_t clock_offset = 0;
while (clock_offset < clock_count)
{
clock_offset = clock() - start_clock;
}
}
extern "C" void
computeGPU(Vector* in1, Vector* in2, Vector* in3, int x)
{
int threadsPerBlock = 256;
int blocksPerGrid = (in1->N + threadsPerBlock - 1) / threadsPerBlock;
compute<<<blocksPerGrid, threadsPerBlock, 0, stream1>>>(in1->d_data, x, 1);
compute<<<blocksPerGrid, threadsPerBlock, 0, stream2>>>(in2->d_data, x, 2);
compute<<<blocksPerGrid, threadsPerBlock, 0, stream3>>>(in3->d_data, x, 3);
}
我的内核现在同时运行,等待三个完成,然后启动接下来的三个,因为我在循环中同步:
testMain.c
int x = 0;
for (x = 0; x < Ncomp; x++)
{
computeGPU(test1, test2, test3, x);
//checkGPU( cudaThreadSynchronize() );
}
checkGPU( cudaThreadSynchronize() );
test.cu
extern "C" void
computeGPU(Vector* in1, Vector* in2, Vector* in3, int x)
{
int threadsPerBlock = 256;
int blocksPerGrid = (in1->N + threadsPerBlock - 1) / threadsPerBlock;
compute<<<blocksPerGrid, threadsPerBlock, 0, stream1>>>(in1->d_data, x, 1);
compute<<<blocksPerGrid, threadsPerBlock, 0, stream1>>>(in1->d_data, x, 1);
compute<<<blocksPerGrid, threadsPerBlock, 0, stream1>>>(in1->d_data, x, 1);
compute<<<blocksPerGrid, threadsPerBlock, 0, stream2>>>(in2->d_data, x, 2);
compute<<<blocksPerGrid, threadsPerBlock, 0, stream3>>>(in3->d_data, x, 3);
}
答案 0 :(得分:0)
http://on-demand.gputechconf.com/gtc-express/2011/presentations/StreamsAndConcurrencyWebinar.pdf
查看幻灯片18,了解有关提交并发内核的有效顺序的说明。
有了音频: https://developer.nvidia.com/gpu-computing-webinars
寻找cuda并发&amp;流。