关于矩阵相乘


#include<stdio.h>
#include<stdlib.h>
#include<CL/cl.hpp>

//矩阵相乘核心算法
const char *KernelSource=
"__kernel void SimpleMultiply(__global int *outputC,int widthA,int heightA,int widthB,int heightB,__global int *inputA,__global int *inputB)"
"{"
"    int row=get_global_id(1);"
"    int col=get_global_id(0);"
"    int sum=0;"
"    for(int i=0;i<widthA;i++)"
"    {"
"        sum+=inputA[row*widthA+i]*inputB[i*widthB+col];"
"     }"
"     outputC[row*widthB+col]=sum;"
"};";

int main()
{
const int heightA=6;
const int widthA=8;
const int heightB=8;
const int widthB=10;
const int heightC=6;
const int widthC=10;
int i,j,k;
int A[heightA][widthA]=
{
1,2,3,4,5,6,7,8,
1,2,3,4,5,6,7,8,
1,2,3,4,5,6,7,8,
1,2,3,4,5,6,7,8,
1,2,3,4,5,6,7,8,
1,2,3,4,5,6,7,8
};
int B[heightB][widthB]=
{
10,9,8,7,6,5,4,3,2,1,
10,9,8,7,6,5,4,3,2,1,
10,9,8,7,6,5,4,3,2,1,
10,9,8,7,6,5,4,3,2,1,
10,9,8,7,6,5,4,3,2,1,
10,9,8,7,6,5,4,3,2,1,
10,9,8,7,6,5,4,3,2,1,
10,9,8,7,6,5,4,3,2,1
};
int C[heightC][widthC];
cl_int err;
cl_platform_id platform;
err=clGetPlatformIDs(1,&platform,NULL);
cl_device_id device;
err=clGetDeviceIDs(platform,CL_DEVICE_TYPE_GPU,1,&device,NULL);
cl_context context;
context=clCreateContext(NULL,1,&device,NULL,NULL,&err);
cl_command_queue cmdQueue;
cmdQueue=clCreateCommandQueue(context,device,NULL,&err);
cl_mem BufferA;
cl_mem BufferB;
cl_mem BufferC;
BufferA=clCreateBuffer(context,CL_MEM_READ_ONLY,sizeof(int)*widthA*heightA,NULL,&err);
BufferB=clCreateBuffer(context,CL_MEM_READ_ONLY,sizeof(int)*widthB*heightB,NULL,&err);
BufferC=clCreateBuffer(context,CL_MEM_WRITE_ONLY,sizeof(int)*widthC*heightC,NULL,&err);
err=clEnqueueWriteBuffer(cmdQueue,BufferA,CL_TRUE,0,sizeof(int)*widthA*heightA,A,0,NULL,NULL);
err=clEnqueueWriteBuffer(cmdQueue,BufferB,CL_TRUE,0,sizeof(int)*widthB*heightB,B,0,NULL,NULL);
cl_program program;
program=clCreateProgramWithSource(context,1,&KernelSource,NULL,&err);
err=clBuildProgram(program,1,&device,NULL,NULL,NULL);
cl_kernel kernel;
kernel=clCreateKernel(program,"SimpleMultiply",&err);
err=clSetKernelArg(kernel,0,sizeof(cl_mem),&BufferC);
err=clSetKernelArg(kernel,1,sizeof(int),&widthA);
err=clSetKernelArg(kernel,2,sizeof(int),&heightA);
err=clSetKernelArg(kernel,3,sizeof(int),&widthB);
err=clSetKernelArg(kernel,4,sizeof(int),&heightB);
err=clSetKernelArg(kernel,5,sizeof(cl_mem),&BufferA);
err=clSetKernelArg(kernel,6,sizeof(cl_mem),&BufferB);
//size_t localWorkSize[2]={16,16};
size_t globalWorkSize[2]={widthC,heightC};
err=clEnqueueNDRangeKernel(cmdQueue,kernel,2,NULL,globalWorkSize,NULL,0,NULL,NULL);
err=clEnqueueReadBuffer(cmdQueue,BufferC,CL_TRUE,0,sizeof(int)*widthC*heightC,C,0,NULL,NULL);
for(i=0;i<heightC;i++)
{
for(j=0;j<widthC;j++)
{
printf("%d ",C[i][j]);
}
printf("\n");
}
system("pause");
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值