15,611,801 members
See more:
can anyone convert this c code into cuda??
```// C code to find the Kronecker Product of two
// matrices and stores it as matrix C
#include <stdio.h>

// rowa and cola are no of rows and columns
// of matrix A
// rowb and colb are no of rows and columns
// of matrix B
const int cola = 2, rowa = 3, colb = 3, rowb = 2;

// Function to computes the Kronecker Product
// of two matrices
void Kroneckerproduct(int A[][cola], int B[][colb])
{

int C[rowa * rowb][cola * colb];

// i loops till rowa
for (int i = 0; i < rowa; i++) {

// k loops till rowb
for (int k = 0; k < rowb; k++) {

// j loops till cola
for (int j = 0; j < cola; j++) {

// l loops till colb
for (int l = 0; l < colb; l++) {

// Each element of matrix A is
// multiplied by whole Matrix B
// resp and stored as Matrix C
C[i + l + 1][j + k + 1] = A[i][j] * B[k][l];
printf("%d\t", C[i + l + 1][j + k + 1]);
}
}
printf("\n");
}
}
}

// Driver Code
int main()
{
int A[3][2] = { { 1, 2 }, { 3, 4 }, { 1, 0 } },
B[2][3] = { { 0, 5, 2 }, { 6, 7, 3 } };

Kroneckerproduct(A, B);
return 0;
} ```

What I have tried:

I have tried for days but always there are some errors and warnings. some error MSB3721 is always there
Posted
Updated 12-May-19 23:19pm
Richard MacCutchan 9-May-19 3:37am
Unless you show us the warnings we cannot guess what the problem is.
Stefan_Lang 9-May-19 3:48am
Step 1: get rid of printf statements!
Step 2: look up the documentation for the errors that you get and check the code at the indicated position
Step 3: Show the code that is producing the error. Nobody can explain to you what you did wrong if you don't show us what you did in the first place.

That said:
I see no point to convert this into CUDA or any other kind of parallel code: the synchronization effort will eat up the trivial amount of processing time you'd save for the calculations. Parallelization only makes sense when you have a meaningful amount of processing to do between synchronization events.
Rick York 9-May-19 16:22pm
Try searching the internet. There's a high probability someone has done it already. I did a quick search and several things popped up.
Kornfeld Eliyahu Peter 12-May-19 6:46am
All MSB3721 means, that your nvcc compiler failed, but the actual details are hidden in your output window... However, it is very possible that without increasing the verbosity of VS it will not show at all... So do it and recompile...
The error just before MSB3721 will give you a hint - Google it...

## Solution 1

I tested your code with Visual Studio 2017 Ultimate, and there are no warnings or issues.

1. Create a Console application
2. Place the following code in your main .cpp file

```// Test1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>

// C code to find the Kronecker Product of two
// matrices and stores it as matrix C
#include <stdio.h>

// rowa and cola are no of rows and columns
// of matrix A
// rowb and colb are no of rows and columns
// of matrix B
const int cola = 2, rowa = 3, colb = 3, rowb = 2;

// Function to computes the Kronecker Product
// of two matrices
void Kroneckerproduct(int A[][cola], int B[][colb])
{

int C[rowa * rowb][cola * colb];

// i loops till rowa
for (int i = 0; i < rowa; i++) {

// k loops till rowb
for (int k = 0; k < rowb; k++) {

// j loops till cola
for (int j = 0; j < cola; j++) {

// l loops till colb
for (int l = 0; l < colb; l++) {

// Each element of matrix A is
// multiplied by whole Matrix B
// resp and stored as Matrix C
C[i + l + 1][j + k + 1] = A[i][j] * B[k][l];
printf("%d\t", C[i + l + 1][j + k + 1]);
}
}
printf("\n");
}
}
}

// Driver Code
int main()
{
int A[3][2] = { { 1, 2 }, { 3, 4 }, { 1, 0 } },
B[2][3] = { { 0, 5, 2 }, { 6, 7, 3 } };

Kroneckerproduct(A, B);
return 0;
}```

Stefan_Lang 13-May-19 4:32am
The issues were not with the code as posted, but with the attempts to convert them to CUDA.
Michael Haephrati 13-May-19 6:43am

## Solution 2

You still haven't posted the code that causes the errors, and the full text of the error (although in case of MSB3721 this may not be helpful - it is a generic error code).

I can only say that the index you use to assign elements in C are incorrect! You may not believe so since your program outputs the correct result, but this is only the case because you print each cell of C right after it's calculated, using the same incorrect index value, and obviously before there is a chance for it to be overwritten!

That the code can't function can be easily seen: the minimum inde values used for C in this code is 1, for both rows and colums, so the first row and column is never assigned! Furthermore, the largest index values are 2+3+1=6 and 3+2+1=6, so some of the values get written into random memory, causing undefined behaviour.

You can check the actual content of C if you add the following loop to the function KroneckerProduct():
C++
```for (int i=0; i < 6; ++i) {
printf("\n");
for (int j=0; j < 6; ++j) {
printf("%d\t", C[i][j]);
}
}
```

You will get something like this:
```-971846352      32519   -343317296      32767   -971795976      32519
64550200        0       6       12      -965645592      32519
0       0       18      24      -965709824      32519
4195187 0       6       0       4195096 0
0       5       7       0       0       0
-343317112      2       3       0       -965708464      32519                                                               ```

The correct product function should be something like this:
C++
```for (int i = 0; i < rowa*rowb; ++i) {
int ai = i/rowb;
int bi = i%rowb;
for (int j = 0; j < cola*colb; ++j) {
int aj = j/colb;
int bj = j%colb;
C[i][j] = A[ai][aj]*B[bi][bj];
}
}
```

This will produce the correct output:
C++
```0       5       2       0       10      4
6       7       3       12      14      6
0       15      6       0       20      8
18      21      9       24      28      12
0       5       2       0       0       0
6       7       3       0       0       0```