我需要帮助为kronecker产品创建cuda代码。有人可以帮忙吗? [英] I need help in creating a cuda code for kronecker product. Can anyone help?

查看:80
本文介绍了我需要帮助为kronecker产品创建cuda代码。有人可以帮忙吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以将此c代码转换为cuda ??

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; 
} 





我的尝试:



我已经试了好几天但总是有一些错误和警告。一些错误MSB3721总是在那里



What I have tried:

I have tried for days but always there are some errors and warnings. some error MSB3721 is always there

推荐答案

我用Visual Studio 2017 Ultimate测试了你的代码,没有任何警告或问题。



1.创建控制台应用程序

2.将以下代码放在主.cpp文件中



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;
}


你还没有发布导致错误的代码和错误的全文(尽管在MSB3721的情况下这是可能没有帮助 - 这是一个通用的错误代码。)



我只能说你用来分配C中元素的索引是不正确的!您可能不相信,因为您的程序输出了正确的结果,但这只是这种情况,因为您使用相同的错误索引值在计算后立即打印C的每个单元格,并且显然在它有可能被覆盖之前!



代码无法正常工作可以很容易看出:对于行和列,此代码中用于C的最小inde值为1,因此永远不会分配第一行和第一行!此外,最大的索引值是2 + 3 + 1 = 6和3 + 2 + 1 = 6,因此一些值被写入随机存储器,导致不确定的行为。



如果将以下循环添加到函数KroneckerProduct()中,您可以检查C的实际内容:

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():
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:

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:

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


这篇关于我需要帮助为kronecker产品创建cuda代码。有人可以帮忙吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆