ここ数か月、並列プログラミングについて勉強してきましたが、今は自分のアプリケーションをマルチ GPU プラットフォームに適応させようとしています。問題は、複数の GPU を使用して配列を反復処理する方法をまだよく理解していないことです。
メイン配列を小さなサブ配列に分割し、それぞれを各 GPU に送信する必要がありますか、または配列のフラグメントで各 GPU を反復させる方法がありますか? このアプリケーションのシリアルおよびシングル GPU バージョンが動作しており、さまざまな方法を使用してこの問題を解決し、マルチ GPU に適応させようとしましたが、いずれも以前の 2 つのバージョンと同じ結果を返しません。これ以上何ができるかわからないので、マルチ GPU システムで配列を反復処理する方法を理解していないというのが私の結論です。誰か助けてくれませんか?
私のコードは N 回の反復を実行し、各反復で (グリッドを表す) 配列の各値を調べて、新しい値を計算します。
これは、私のコードが現在どのように見えるかのスケッチです。
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#define DIM 24
#define BLOCK_SIZE 16
#define SRAND_VALUE 585
__global__ void random(int* t, int* newT){
int iy = blockDim.y * blockIdx.y + threadIdx.y + 1;
int ix = blockDim.x * blockIdx.x + threadIdx.x + 1;
int id = iy * (dim+2) + ix;
if (iy <= DIM && ix <= DIM) {
if (t[id] % 2 == 0)
newT[id] = t[id]*3;
else
newT[id] = t[id]*5;
}
}
int main(int argc, char* argv[]){
int i,j, devCount;
int *h_test, *d_test, *d_tempTest, *d_newTest;
size_t gridBytes;
cudaGetDeviceCount(&devCount);
gridBytes = sizeof(int)*(DIM)*(DIM);
h_test = (int*)malloc(gridBytes);
srand(SRAND_VALUE);
#pragma omp parallel for private(i,j)
for(i = 1; i<=DIM;i++) {
for(j = 1; j<=DIM; j++) {
h_test[i*(DIM)+j] = rand() % 2;
}
}
if (devCount == 0){
printf("There are no devices in this machine!");
return 1; // if there is no GPU, then break the code
}
dim3 blockSize(BLOCK_SIZE, BLOCK_SIZE,1);
int linGrid = (int)ceil(DIM/(float)BLOCK_SIZE);
dim3 gridSize(linGrid,linGrid,1);
dim3 cpyBlockSize(BLOCK_SIZE,1,1);
dim3 cpyGridRowsGridSize((int)ceil(DIM/(float)cpyBlockSize.x),1,1);
dim3 cpyGridColsGridSize((int)ceil((DIM+2)/(float)cpyBlockSize.x),1,1);
else if (devCount == 1){
cudaMalloc(&d_test, gridBytes);
cudaMalloc(&d_tempTest, gridBytes);
cudaMalloc(&d_newTest, gridBytes);
cudaMemcpy(d_test, h_test, gridBytes, cudaMemcpyHostToDevice);
for (iter = 0; iter < DIM; iter ++){
random<<<gridSize, blockSize>>>(d_test, d_newTest);
d_tempTest = d_test;
d_test = d_newTest;
d_newTest = d_tempTest;
}
cudaMemcpy(h_test, d_test, gridBytes, cudaMemcpyDeviceToHost);
return 0;
}
else{
int nThreads, tId, current;
omp_set_num_threads(devCount);
for (iter = 0; iter < DIM; iter ++){
#pragma omp parallel private(tId, h_subGrid, ) shared(h_grid, gridBytes)
{
tId = omp_get_thread_num();
cudaSetDevice(tId);
cudaMalloc(&d_test, gridBytes);
cudaMalloc(&d_tempTest, gridBytes);
cudaMalloc(&d_newTest, gridBytes);
cudaMemcpy(d_grid, h_grid, gridBytes, cudaMemcpyHostToDevice);
******// What do I do here//******
}
}
return 0;
}
}
前もって感謝します。