CUDAで行列計算をしたいのですがCUBLASがエラー出力する

このQ&Aのポイント
  • CUDAで行列計算を行いたいのですが,エラーが出てしまいます.
  • 変数の型の互換性の問題のようなのですが...
  • プログラムとエラー構文を添付しておきます.
回答を見る
  • ベストアンサー

CUDAで行列計算をしたいのですがCUBLASが

CUDAで行列計算を行いたいのですが,エラーが出てしまいます. 原因がわからないので,わかる方いらっしゃいましたら教えてください. 変数の型の互換性の問題のようなのですが... プログラムとエラー構文を添付しておきます. 環境はNVDIA Tesla K20,CUDA5.5,Ubuntu12.04です. #include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <cublas.h> #define N 1000 #define M 1500 #define K 500 int main(int args , char **argv) { double alpha = 3.0 , beta = 2.0; double *hMatA , *hMatB , *hMatC; double *dMatA , *dMatB , *dMatC; int LDA = M , LDB = K ,LDC = M ; int i; cudaSetDevice(0); cublasInit(); //CUBLASライブラリの初期化 cudaMallocHost((void**)&hMatA,sizeof(double)*M*K); cudaMallocHost((void**)&hMatB,sizeof(double)*K*M); cudaMallocHost((void**)&hMatC,sizeof(double)*M*N); for(i=0;i<M*K;i++) { hMatA[i] = double(i+1); } for(i=0;i<K*N;i++) { hMatB[i] = double(i+1); } for(i=0;i<M*N;i++) { hMatC[i] = double(i+1); } cublasAlloc(M*K,sizeof(double),(void**)&dMatA); //CUBLASライブラリ用にデバイス・メモリを確保 cublasAlloc(K*N,sizeof(double),(void**)&dMatB); cublasAlloc(M*N,sizeof(double),(void**)&dMatC); /* cublasSetMatrix(M*K,sizeof(double),hMatA,LDA,dMatA,M); //デバイス・メモリへ必要な部分を転送 cublasSetMatrix(K*N,sizeof(double),hMatB,LDB,dMatB,K); cublasSetMatrix(M*N,sizeof(double),hMatC,LDC,dMatC,M); */ cublasSetMatrix(M*K,sizeof(double),hMatA,LDA,dMatA,M); cublasSetMatrix(K*N,sizeof(double),hMatB,LDB,dMatB,K); cublasSetMatrix(M*N,sizeof(double),hMatC,LDC,dMatC,M); cublasDgemm('N','N',M,N,K,alpha,dMatA,M,dMatB,K,beta,dMatC,M); //DGEMMルーチンを呼び出す cublasGetMatrix(M,N,sizeof(double),dMatC,M,hMatC,LDC); cublasFree(dMatA); cublasFree(dMatB); cublasFree(dMatC); cublasShutdown(); } 以下エラー構文 testinv.cu(44): error: argument of type "double *" is incompatible with parameter of type "int" testinv.cu(44): error: argument of type "int" is incompatible with parameter of type "const void *" testinv.cu(44): error: argument of type "double *" is incompatible with parameter of type "int" testinv.cu(44): error: argument of type "int" is incompatible with parameter of type "void *" testinv.cu(44): error: too few arguments in function call testinv.cu(45): error: argument of type "double *" is incompatible with parameter of type "int" testinv.cu(45): error: argument of type "int" is incompatible with parameter of type "const void *" testinv.cu(45): error: argument of type "double *" is incompatible with parameter of type "int" testinv.cu(45): error: argument of type "int" is incompatible with parameter of type "void *" testinv.cu(45): error: too few arguments in function call testinv.cu(46): error: argument of type "double *" is incompatible with parameter of type "int" testinv.cu(46): error: argument of type "int" is incompatible with parameter of type "const void *" testinv.cu(46): error: argument of type "double *" is incompatible with parameter of type "int" testinv.cu(46): error: argument of type "int" is incompatible with parameter of type "void *" testinv.cu(46): error: too few arguments in function call 15 errors detected in the compilation of "/tmp/tmpxft_00001df9_00000000-6_testinv.cpp1.ii".

質問者が選んだベストアンサー

  • ベストアンサー
  • ki073
  • ベストアンサー率77% (491/634)
回答No.1

cublasSetMatrix(M*K,sizeof(double),hMatA,LDA,dMatA,M); から3行あたりでエラーが出ています。 引数の数が間違っているようです。以下を参考にしてみてください。 http://d.hatena.ne.jp/ground256/20101011/1286813176

taiti_023
質問者

お礼

無事解決することができました!!! またなんかあったらお願いします!

関連するQ&A

  • 行列の和のプログラミング

    以下のをCソースを打ち込んでコンパイルすると、 warning: passing arg 1 of `mat_add' from incompatible pointer type warning: passing arg 2 of `mat_add' from incompatible pointer type となります。これは、どういうエラーでどう直せばいいのでしょうか? #include <stdio.h> void mat_add(const int ma[2][3], const int mb[2][3], int mc[2][3]) { int i, j; for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) mc[i][j] = ma[i][j] + mb[i][j]; } int main(void) { int i, j; int ma[2][3] = { {10, -5, 4}, {18, -2, -18} }; int mb[2][3] = { {23, 16, -9}, {-3, 20, 5} }; int mc[2][3] = { 0 }; mat_add(ma, mb, mc); for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) printf("%3d", mc[i][j]); putchar('\n'); } return (0); }

  • 行列の計算

    #include<stdio.h> #define N 2 #define M 3 void hyoji(float[][M]); int main(){ int i,j,k; float a[N][M] = {{2.0,2.0,2.0},{2.0,2.0,2.0}}; float b[M][M] = {{1.0,1.0,1.0},{2.0,2.0,2.0},{1.0,1.0,1.0}}; float c[N][N]; for(i=0; i<N; i++){ for(j=0; j<M; j++){ c[i][j] = 0; for(k=0; k<M; k++){ c[i][j] += a[i][k] * b[k][j]; } } } hyoji(c); return(0); } void hyoji(float x[][M]){ int i,j; for(i=0; i<N; i++){ for(j=0; j<M; j++){ printf("%4.1f ",x[i][j]); } printf("\n"); } } 以上のプログラムで 行列aと行列bをかけ合せた行列cを求めるのですが コンパイルすると 8 8 8 8 8 1 となり、正しい結果がでません。 なにが間違っているのでしょうか?? よろしくお願いします。

  • 自作の行列クラスを継承するさいにエラーがでます

    現在、c++の学習で、自作の基底行列クラスMatrixを作成し、このクラスを継承して新たにlduMatrixを作成する事を考えています。 が、継承し、 lduMatrix a( 3, 3); a = 3; とすると、 main.C:13: warning: passing ‘double’ for argument 1 to ‘lduMatrix::lduMatrix(int)’ というエラーがでてコンパイルできずにいます。その一方で、 lduMatrix a(3,3, 3.14); とするとコンパイルはとおり、lduMatrix行列の各要素(a[1][1]など)の値をプリントさせると、[[3.14]]の値が入っていることを確認しております。 どこが間違っているのか御指導いただけると幸いです。 以下、クラスの中身です。よろしくおねがいします。 《Class: Matrix》 #include <iostream> class Matrix{ private: //! Size of row and column in Matrix int row_, col_; //! Row pointers double** m_; //! Allocate function for row-pointers void allocate(); public: Matrix(); //! Constructor with given matrix size Matrix( const int, const int ); //! Constructor with given matrix size and value fro all elements Matrix( const int, const int, const double ); //! Destructor ~Matrix(); ・・・省略・・・ double* operator[]( const int ); double* operator[]( const int ) const ; void operator=( const double ); }; /* Private functions *********************************************** */ void Matrix::allocate() { m_ = new double* [row_]; m_[0] = new double [row_*col_]; for ( int i=1; i<row_; i++ ){ m_[i] = m_[i-1] + col_; } } /* Destructor ****************************************************** */ Matrix::~Matrix(){ delete[] m_[0]; delete[] m_; } /* Constructors **************************************************** */ // NULL constructor Matrix::Matrix() : row_(0), col_(0), m_(NULL) {} // Constructor with given matrix size Matrix::Matrix( const int row, const int col ) : row_(row), col_(col) { allocate(); } // Constructor with given matrix size and value for all elements Matrix::Matrix( const int row, const int col, const double s ): row_(row), col_(col) { allocate(); double* m = m_[0]; for ( int i=0; i<row_*col_; i++ ){ m[i] = s; } } 《省略》 /* Member operators ************************************************ */ double* Matrix::operator[]( const int i ){ return m_[i]; } void Matrix::operator=( const double t ){ double* m = m_[0]; int nm = row_*col_; for ( int i=0; i<nm; i++ ) { m[i] = t; } } 《Class: lduMatrix》 class lduMatrix : public Matrix{ public: lduMatrix(); lduMatrix( const int ); lduMatrix( const int, const double ); }; lduMatrix::lduMatrix() {} lduMatrix::lduMatrix( const int mSize ) : Matrix( mSize, mSize, 0.0 ) {} lduMatrix::lduMatrix( const int mSize, const double s ) : Matrix( mSize, mSize, s ) {}

  • incompatible pointer type

    incompatible pointer type への対応方法を教えてください。 環境はMicrochipのXC16 Compiler V1.24です。 エラーの出る行は putsUART1("UART1 Test"); エラーは main.c:76:9: warning: passing argument 1 of 'putsUART1' from incompatible pointer type c:\program files (x86)\microchip\xc16\v1.24\bin\bin\../..\support\peripheral_24F/uart.h:396:45: note: expected 'unsigned int *' but argument is of type 'char *' となっています。 uart.hを見ると void __attribute__ ((section (".libperi"))) putsUART1(unsigned int *buffer); とあるので、求められているものと違う形のものを渡してしまっていることは分かります。 ところが、具体的にどう変更すればいいのかわかりません。 プログラムをどのように書き直したらエラーを無くすことが出来ますか? よろしくお願いします。

  • 多次元配列の受渡しでの警告

    多次元配列を関数に受け渡す際に、 警告: passing argument 1 of ‘mat_add’ from incompatible pointer type 警告: passing argument 2 of ‘mat_add’ from incompatible pointer type のような警告が出てきてしまいます。 プログラムは、明解C言語(柴田望洋著)で紹介されているサンプルプログラムなのですが(下に載せておきます)、どうしてこのような警告がでるのか分からず困っています。 多次元配列の渡し方になにか問題があるのでしょうか? #include <stdio.h> void mat_add(const int ma[][3], const int mb[][3], int mc[][3]) { int i,j; for(i=0; i<2; i++) for(j=0; j<3; j++) mc[i][j] = ma[i][j] + mb[i][j]; } int main(void) { int i,j; int ma[2][3] = {{9,2,-3},{4,5,1}}; int mb[2][3] = {{9,2,-3},{4,5,1}}; int mc[2][3] = {0}; mat_add(ma, mb, mc); for(i=0; i<2; i++){ for(j=0; j<3; j++) printf("%3d",mc[i][j]); putchar('\n'); } return(0); }

  • CからVB

    以下のプログラムはC言語で作成されています。 これをVBで作成したいです。 教えてください。 #include <stdio.h> #include <stdlib.h> double *alloc(int r,int n,int m); void input(double *p,int r,int n,int m); void sumup(double *p,int r,int n,int m); int main(void){ int r, n, m; double *p; // 行列サイズ入力 printf("input r n m "); scanf("%d %d %d",&r,&n,&m); // 行列領域取得 p = alloc(r,n,m); // 行列要素入力 input(p,r,n,m); // 行列の和 sumup(p,r,n,m); //終了,行列領域解放 free(p); return 0; } double *alloc(int r,int n,int m){ double *p; printf("MATRIX[r=%d][n=%d][m=%d]\n\n",r,n,m); p = (double*)malloc(sizeof(double)*r*n*m); if( p == NULL){ printf("error! malloc failed.\n"); exit(-1); } return p; } void input(double *p,int r, int n,int m) { int i,j,k; for(i = 0; i < r; i++){ for(j = 0;j < n; j++){ for(k = 0; k < m; k++){ printf("input MATRIX[%d][%d][%d]= ",i,j,k); scanf("%lf",&p[i*(n*m)+j*m+k]); } } } printf("\n"); } // 行列の和 void sumup(double *p,int r,int n,int m){ int i, j, k; // 0.0, not 0!!! double sum = 0.0; printf("sum of %d matrices:\n",r); for(j = 0; j< n; j++){ for(k = 0;k < m; k++){ sum=0; for(i = 0;i < r;i++){ sum += p[i*(n*m)+j*m+k]; } printf("\n%2f",sum); } } printf("\n"); }

  • C言語で行列の積を計算できるような関数を作って疑問に思ったことがありま

    C言語で行列の積を計算できるような関数を作って疑問に思ったことがあります。 まず↓のような2x3行列と3x2行列が計算できる関数を作りました、、 #include <stdio.h> void mul(const int ma[2][3],const int mb[3][2],int mc[2][2]) { int i,j,k; for(i = 0; i < 2; i++) for(j = 0;j < 2;j++) for(k=0;k<3;k++) mc[i][j]+=ma[i][k]*mb[k][j]; } int main(void) { int i,j; int ma[2][3] ={{1,2,3},{4,5,6}}; int mb[3][2] ={{7,8},{9,0},{1,2}}; int mc[2][2] ={0}; mul(ma,mb,mc); for(i = 0;i < 2; i++) { for(j = 0;j < 2;j++) printf("%4d",mc[i][j]); putchar('\n'); } eturn(0); } ---------------------------------- これをmxn,nxp行列で計算できるような関数にしたいと思い 下のようにしたのですがエラーになります。どうしたらいいでしょうか・・? #include <stdio.h> void mul(const int ma[int m][int n],const int mb[int n][int p],int mc[m][p]) { int i,j,k; for(i=0;i<m;i++) for(j=0;j<p;j++) for(k=0;k<n;k++) mc[i][j]+=ma[i][k]*mb[k][j]; } int main(void) {int i,j; int ma[2][3] ={{1,2,3},{4,5,6}}; int mb[3][2] ={{7,8},{9,0},{1,2}}; int mc[2][2] ={0}; mul(ma,mb,mc); for(i = 0;i < 2; i++) {for(j = 0;j < 2;j++) printf("%4d",mc[i][j]); putchar('\n');} return(0);}

  • 2つのvoid関数でmallocを使うと2step目でセグメントエラーが出る

    プログラムのmainを見やすくするためにサブルーチンとしてvoid関数を使っているのですが、その中でmallocでメモリを確保して配列を作ろうとするとエラーが出てしまいます。概略を書くと #include <stdio.h> #include <math.h> #include <stdlib.h> //voidの宣言 void a(double* ,double*, double,); void b(double* ,double*, double,); main(){ double *c,*d,e; double *f,*g,h; c=(double *)malloc(sizeof(double)*N) //Nは適当な整数です d,f,gも同様の処理 色々な作業 while(適当回数繰り返します){ a(c,d,e); b(f,g,h); 色々な作業 } } void a (double* c, double* d, double e){ double **i,*j,; int k; i=(double **)malloc(sizeof(double *)*M) //Mは適当な整数です for(k=0;k<=M;k++){ i[k]=(double *)malloc(sizeof(double)*M) //Mは適当な整数です } jも同様にメモリ確保 様々な作業(逆行列の計算など) } void bはaとほとんど同じです。 mallocで二次元配列や配列を作っています。勿論同じ文字は使っていません。 以上のようなプログラムでgccは通ります。でも実行するとwhile内での1step目は上手くいくのですが、2step目のb内での始めの(double *)malloc(sizeof(double)*L)(Lは適当な整数です)で、つまりメモリ確保でセグメントエラーが出ます。両者とも片方を削るとエラーは出ません。1step目は何故上手くいくのか、2ステップ目で何故ダメなのかが分かりません。皆様の御教授をお願いします。

  • 行列の積を計算するプログラムがうまくいきません

    どこが間違っているのかわかる方お願いします ・行列A,Bはファイルから読み込む ・行列A,Bの積Cの計算には関数を用いる #include<stdio.h> #define ROW 10 #define COL 10 void MatrixProduct(int a[][COL],int b[][ROW],int c[][ROW],int n,int m ) { int i,j,k; for(i=0;i<n;i++){ for(j=0;j<n;j++){ c[i][j]=0; } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ for(k=0;k<m;k++){ c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } } int main(void) { FILE *fp1,*fp2; char fname1[64],fname2[64]; int a[ROW][COL],b[ROW][COL],c[ROW][COL],n,m; int i,j,k; printf("Input file name ?"); scanf("%s",fname1); printf("Output file name ?"); scanf("%s",fname2); fp1=fopen(fname1,"r"); fp2=fopen(fname2,"w"); fscanf(fp1,"%d %d",&n,&m); MatrixProduct(a,b,c,n,m); for(i=0;i<n;i++){ for(j=0;j<n;j++){ fprintf(fp2,"%3d",c[i][j]); } fprintf(fp2,"\n"); } fclose(fp1); fclose(fp2); return(0); } fp1 3 4 1 2 3 4 2 3 4 5 3 4 5 6 1 2 3 2 3 4 3 4 5 4 5 6

  • 関数への構造体の配列の渡し方<c言語初心者>

    こんにちは、関数への構造体の配列の渡し方で理解できない点があるため、質問させていただきます。 以下がスクリプトになります。3人の名前と年齢をinput関数で入力し、それらのデータをoutput関数で出力するのが目的です。 #include <stdio.h> typedef struct{ char name[64]; int age; }property; void input(property *data[]); void output(property *data[]); int main(void){ property data[3]; printf("Input data of three people.\n"); input(&data); output(&data); return 0; } void input(property *data[]){ int i; for(i=0;i<3;i++){ printf("%d banme\n",i+1); printf("name:"); scanf("%s",&data[i]->name); printf(" age:"); scanf("%3d",&data[i]->age); } return; } void output(property *data[]){ int i; for(i=0;i<3;i++){ printf("%d banme\n",i+1); printf("name:%s\n",data[i]->name); printf("age :%3d\n",data[i]->age); } return; } コンパイル時のエラーメッセージは以下のようになりました。(ファイル名はstructure5.c) structure5.c: In function ‘main’: structure5.c:14:2: warning: passing argument 1 of ‘input’ from incompatible pointer type [enabled by default] input(&data); ^ structure5.c:8:6: note: expected ‘struct property **’ but argument is of type ‘struct property (*)[3]’ void input(property *data[]); ^ structure5.c:15:2: warning: passing argument 1 of ‘output’ from incompatible pointer type [enabled by default] output(&data); ^ structure5.c:9:6: note: expected ‘struct property **’ but argument is of type ‘struct property (*)[3]’ void output(property *data[]); ^ structure5.c: In function ‘input’: structure5.c:24:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[64]’ [-Wformat=] scanf("%s",&data[i]->name); ^ 構造体の配列をinput関数やoutput関数に渡すときにエラーが発生しているようなのですが、自分で調べても解決できなかったため、質問させて頂きます。 皆様のお知恵を貸してください。なおプログラミング言語自体初心者のため、できる限りわかりやすいお言葉でご教授願います。よろしくお願い致します。

専門家に質問してみよう