Very Basic newb Programming Question in C++ Passing Matrices from Programming Guide

Hello All,

In chapter 6 of the programming guide (version 2.1), there is a source code listing which has basic matrix multiplication. What I am trying to do is add a main() function to call the Mul(float *A, …) function. I wanted to explicitly declare the arrays inside main and then send them to the function. The problem is that I am new to C++ and new to CUDA and haven’t found this as easy as expected. Could someone write a suggestion of a simple main function that would do this.

What I had tried to do was

float *A[3][2];

A[0][0] = 1; A[0][1] = 0;

This definitely doesn’t work. If anyone can tolerate dealing with someone who doesn’t know enough C++, please help me out.


Perhaps instead try

float A[3][2];

Of course with such a small matrix it will not see any performance improvement, but it should compile. The code requires the matrix dimensions to be a multiple of BLOCK_SIZE, but you should be able to set BLOCK_SIZE to 1 and it should execute.

CORRECTION: The above comment did not completely solve the issue, please read the problem below.

That makes a lot of sense ( I feel dumb). Thank you for replying.

I had set it up as a float *A just because thats what the function was expecting, but I don’t need to do that as long as I pass it correctly I suppose.’

Thanks again.

There were problems with the suggested implementation above. So here is what my main function looks like now:


int main(int argc, char** argv)


float (*A)[3] = new float[2][3];

float (*B)[2] = new float[3][2];

float (*C)[2] = new float[2][2];

A[0][0] = 1.0f;  A[0][1] = 0.0f; A[0][2] = 2.0f;

A[1][0] = -1.0f; A[1][1] = 3.0f; A[1][2] = 1.0f;

B[0][0] = 3.0f; B[0][1] = 1.0f;

B[1][0] = 2.0f; B[1][1] = 1.0f;

B[2][0] = 1.0f; B[2][1] = 0.0f;

//Mul(const float *A, const float *B, int hA, int wA, int wB, float *C)

Mul(*A, *B, 2, 3, 2, *C);

for (int i = 0; i < 2; i++)


	for(int j = 0; j < 2; j++)


		printf("%f ", C[i][j] );




CUT_EXIT(argc, argv);



The issue I am getting is that I would think the above matrices would be

A =

[1 0 2

-1 3 1]

B =

[3 1

2 1

1 0]

And therefore, I think

A * B =

[5 1

4 2]

But, the answer I am getting from the above code is:

[5.000000 1.997346

4.000000 0.998673]

or roughly,

[5 2

4 1]

Can anyone tell me if I am either doing the matrix math in my head wrong or if my code is set up incorrectly?