- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 CUDA 实现矩阵乘法后。我尝试用CUBLAS实现它(感谢论坛中一些人的建议)。
我可以乘方阵,但是(是的,再次......)我在处理非方阵时遇到困难。唯一有效的非方阵乘法类型是当您改变矩阵 A 的宽度(A*B=C) 时。
我没有收到任何错误,但结果矩阵返回错误的值。这是我的代码(它基本上是 simpleCUBLAS SDK 示例的改编):
#include <stdlib.h>
#include <stdio.h>
#include "cublas.h"
#define HA 2
#define WA 9
#define WB 2
#define HB WA
#define WC WB
#define HC HA
#define index(i,j,ld) (((j)*(ld))+(i))
void printMat(float*P,int uWP,int uHP){
//printf("\n %f",P[1]);
int i,j;
for(i=0;i<uHP;i++){
printf("\n");
for(j=0;j<uWP;j++)
printf("%f ",P[index(i,j,uHP)]);
//printf("%f ",P[i*uWP+j]);
}
}
int main (int argc, char** argv) {
cublasStatus status;
int i,j;
cublasInit();
float *A = (float*)malloc(HA*WA*sizeof(float));
float *B = (float*)malloc(HB*WB*sizeof(float));
float *C = (float*)malloc(HC*WC*sizeof(float));
if (A == 0) {
fprintf (stderr, "!!!! host memory allocation error (A)\n");
return EXIT_FAILURE;
}
if (B == 0) {
fprintf (stderr, "!!!! host memory allocation error (A)\n");
return EXIT_FAILURE;
}
if (C == 0) {
fprintf (stderr, "!!!! host memory allocation error (A)\n");
return EXIT_FAILURE;
}
for (i=0;i<HA;i++)
for (j=0;j<WA;j++)
A[index(i,j,HA)] = (float) index(i,j,HA);
for (i=0;i<HB;i++)
for (j=0;j<WB;j++)
B[index(i,j,HB)] = (float) index(i,j,HB);
/*
for (i=0;i<HA*WA;i++)
A[i]=(float) i;
for (i=0;i<HB*WB;i++)
B[i]=(float) i; */
float* AA; float* BB; float* CC;
/*ALLOCATE ON THE DEVICE*/
status=cublasAlloc(HA*WA,sizeof(float),(void**)&AA);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf (stderr, "!!!! device memory allocation error (A)\n");
return EXIT_FAILURE;
}
status=cublasAlloc(HB*WB,sizeof(float),(void**)&BB);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf (stderr, "!!!! device memory allocation error (A)\n");
return EXIT_FAILURE;
}
status=cublasAlloc(HC*WC,sizeof(float),(void**)&CC);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf (stderr, "!!!! device memory allocation error (A)\n");
return EXIT_FAILURE;
}
/*SET MATRIX*/
status=cublasSetMatrix(HA,WA,sizeof(float),A,HA,AA,HA);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf (stderr, "!!!! device memory allocation error (A)\n");
return EXIT_FAILURE;
}
status=cublasSetMatrix(HB,WB,sizeof(float),B,HB,BB,HB);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf (stderr, "!!!! device memory allocation error (A)\n");
return EXIT_FAILURE;
}
/*KERNEL*/
cublasSgemm('n','n',HA,WB,WA,1,AA,HA,BB,HB,0,CC,HC);
status = cublasGetError();
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf (stderr, "!!!! kernel execution error.\n");
return EXIT_FAILURE;
}
cublasGetMatrix(HC,WC,sizeof(float),CC,HC,C,HC);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf (stderr, "!!!! device read error (A)\n");
return EXIT_FAILURE;
}
/* PERFORMANCE OUTPUT*/
printf("\nMatriz A:\n");
printMat(A,WA,HA);
printf("\nMatriz B:\n");
printMat(B,WB,HB);
printf("\nMatriz C:\n");
printMat(C,WC,HC);
free( A ); free( B ); free ( C );
status = cublasFree(AA);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf (stderr, "!!!! memory free error (A)\n");
return EXIT_FAILURE;
}
status = cublasFree(BB);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf (stderr, "!!!! memory free error (B)\n");
return EXIT_FAILURE;
}
status = cublasFree(CC);
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf (stderr, "!!!! memory free error (C)\n");
return EXIT_FAILURE;
}
/* Shutdown */
status = cublasShutdown();
if (status != CUBLAS_STATUS_SUCCESS) {
fprintf (stderr, "!!!! shutdown error (A)\n");
return EXIT_FAILURE;
}
if (argc > 1) {
if (!strcmp(argv[1], "-noprompt") ||!strcmp(argv[1], "-qatest") )
{
return EXIT_SUCCESS;
}
}
else
{
printf("\nPress ENTER to exit...\n");
getchar();
}
return EXIT_SUCCESS;
}
有什么想法吗?另外,有没有人在 CUBLAS 中有一个可以工作的矩阵乘法实现,所以我可以比较?提前致谢。
最佳答案
我不明白为什么您认为您发布的代码不起作用。当我编译并运行它时,生成的可执行文件产生的输出与我在 matlab 中输入相同的矩阵并计算它们的乘积时得到的输出相同。
CUBLAS 是 FORTRAN BLAS,它需要按列主要顺序输入(并且您的代码是列主要顺序)。如果结果与您想要的不匹配,则您一定在某处混淆了列和行的主要排序。
关于cuda - CUBLAS 矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5551020/
只是一个关于 cublas 的一般问题。对于单线程,如果没有从 GPU 到 CPU 的内存传输(例如 cublasGetVector),cublas 内核函数(例如 cublasDgemm)是否会自动
我已经编写了一个struct 和一些包装“CUBLAS 矩阵对象”的函数 struct 是: #include #include #include #define uint unsigned i
我正在尝试用 cublas 替换我的 gpu block 矩阵乘法,但我在 2x2 测试用例中没有得到我期望的结果: #include "cuda_runtime.h" #include "cubla
我在 Stack Overflow 上阅读了两篇文章,即 Will the cublas kernel functions automatically be synchronized with the
如何检查是否安装了 cuBLAS。有没有一种简单的方法可以使用命令行来完成它而无需实际运行任何 cuda 代码行 最佳答案 尝试一下 cat /usr/local/cuda/include/cubla
我正在使用 CUBLAS(Cuda Blas 库)进行矩阵运算。 是否可以使用 CUBLAS 来实现矩阵项的求幂/均方根? 我的意思是,有 2x2 矩阵 1 4 9 16 我想要的是一个提升到给定值的
我目前正尝试在我的 GPU 上使用 CUBLAS 实现矩阵乘法。 它适用于方矩阵和特定大小的输入,但对于其他输入,最后一行不会返回(并且包含 0,因为这是我实现它的方式)。 我认为这是 cublasS
我想异步调用 cuBLAS 例程。是否可以?如果是,我怎样才能实现这一目标? 最佳答案 在 cublas 调用之前使用 cublasSetStream 函数。 cublasSetStream(cubl
CUBLAS 文档提到我们在读取标量结果之前需要同步: “此外,少数返回标量结果的函数,例如 amax()、amin、asum()、rotg()、rotmg()、dot() 和 nrm2(),通过引用
CUBLAS 是一个异步库。传递给 CUBLAS 的参数对内存所有权有什么要求? 很明显,在异步调用完成之前,不应释放由 CUBLAS 操作的矩阵 - 但标量参数呢? 例如,下面的代码是声音: //.
当我在集群上成功安装tensorflow时,我立即运行mnist demo来检查它是否顺利,但这里我遇到了一个问题。我不知道这是什么意思,但看起来错误来自 CUDA python3 -m tensor
使用 CUDA 实现矩阵乘法后。我尝试用CUBLAS实现它(感谢论坛中一些人的建议)。 我可以乘方阵,但是(是的,再次......)我在处理非方阵时遇到困难。唯一有效的非方阵乘法类型是当您改变矩阵 A
从 CUDA 5.5 开始,CUBLAS 库包含用于批量矩阵分解和求逆的例程(分别为 cublasgetrfBatched 和 cublasgetriBatched )。 从文档中获取指南,我编写了一
我已经实现了以下 CUDA 代码,但我对行为有点困惑。 #include #include #include #include #include "cublas_v2.h" #include
例如, cublasgeam() 会做: 但是如果我想将结果存储在 A 中怎么办?不管怎样?我可以用指针调用它吗 *C = *A这样: 不用担心我可能会将输出写入矩阵,但仍将其作为输入读取?? 如果是
对于矩阵A,documentation仅说明相应的前导维度参数 lda 指的是: leading dimension of two-dimensional array used to store th
我正在尝试从设备运行矩阵求逆。如果从主机调用,此逻辑工作正常。 编译行如下(Linux): nvcc -ccbin g++ -arch=sm_35 -rdc=true simple-inv.cu -o
在 cuBLAS 中,cublasIsamin()给出单精度数组的 argmin。 这是完整的函数声明:cublasStatus_t cublasIsamin(cublasHandle_t handl
我想知道 NVIDIA 的 cuBLAS 库。有没有人有这方面的经验?例如,如果我使用 BLAS 编写一个 C 程序,我是否能够用对 cuBLAS 的调用替换对 BLAS 的调用?或者甚至更好地实现一
这些是我在 4 个 GPU 上运行 cublas DGEMM 的结果,每个 GPU 使用 2 个流(Tesla M2050): 我已经测试了我的结果,它们没问题;与使用默认流的版本相比,我担心我获得的
我是一名优秀的程序员,十分优秀!