[問題] 關於CUDA/C的問題...
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 )
( 未必需要依照此格式,文章條理清楚即可 )
遇到的問題: (題意請描述清楚)
看CUDA程式碼時看到關於Kernel處理陣列的方式有疑問...
希望得到的正確結果:
在kernel能正確執行陣列運算
程式跑出來的錯誤結果:
invalid types 'int[int]' for array subscript(單用pointer function,在function
中用 a[i][j]這樣的宣告)
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
Windows 7 + Dev C++ V4.9.9.2 不過我寫的是c code.
有問題的code: (請善用置底文標色功能)
__global__ void MatrixMulKernel(float* Md, float* Nd, float* Pd, intWidth)
{
// Calculate the row index of the Pd element and M
int Row = blockId.y * TILE_WIDTH + threadId.y;
// Calculate the column idenx of Pd and N
Int Col = blockId.x * TILE_WIDTH + threadId.x;
Pvalue = 0;
// each thread computes one element of the block sub-matrix
for (int k = 0; k < Width; ++k)
Pvalue += Md[Row][k] * Nd[k][Col]; //單看程式碼產生的問題
Pd[Row][Col] = Pvalue; //這一行也是
}
補充說明:
在看CUDA的程式碼的時候產生的問題...他在設計kernel的時候傳入的是pointer,確能在
裡面以一般二維陣列的方式使用?!
一般的c function我試過下列的寫法:
宣告一二維陣列 m[3][3];
int* a;
a=&m[0][0]; //a指向m陣列的第一個位置
void f( int t[][] , int row , int col) //compile不過,會要求你指定後面index
void f( int* a , int row , int col) //單用pointer,沒有問題
{
int i,j;
for(i=0;i<row;i++ )
{
for(j=0; j<col; j++)
printf("%d ", *(a+col*i+j));
}
}
//用double pointer的話:
pt_a = (int**) malloc(4 * sizeof(int*));
int i;
for( i=0 ; i<4 ; i++)
{
pt_a[i]= (int* )malloc( 4 * sizeof(int));
}
void f(int** a , int row, int col) //這樣我也試過.可以!
{
for(int i=0 ; i<row; i++)
{
for(int j=0; j<col)
printf("%d ", a[i][j]);
}
}
不過這就表示要傳2d陣列進function裡要用double pointer,有任何可能用pointer就
做到嗎(當然除了我上面的例子之外)?有可能用
f(int a[][], int row , int col)這種方式做到嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 72.229.53.41
※ 編輯: doomhammer 來自: 72.229.53.41 (11/01 13:06)
→
11/01 20:18, , 1F
11/01 20:18, 1F
→
11/01 20:19, , 2F
11/01 20:19, 2F
→
11/01 22:46, , 3F
11/01 22:46, 3F
→
11/01 22:47, , 4F
11/01 22:47, 4F
→
11/01 22:48, , 5F
11/01 22:48, 5F
→
11/01 22:48, , 6F
11/01 22:48, 6F
→
11/01 22:49, , 7F
11/01 22:49, 7F
→
11/01 22:51, , 8F
11/01 22:51, 8F
→
11/01 22:52, , 9F
11/01 22:52, 9F
→
11/01 22:53, , 10F
11/01 22:53, 10F
→
11/01 22:58, , 11F
11/01 22:58, 11F
→
11/01 22:59, , 12F
11/01 22:59, 12F
→
11/01 22:59, , 13F
11/01 22:59, 13F