我有2个不同的程序.
首先使用opencl进行矩阵 - 矩阵乘法.在我的GPU上它会产生更好的结果,然后在主机CPU上(例如0.2秒对18秒).
第二个使用opencl进行矩阵向量乘法,它在GPU上工作稍慢,然后在主机CPU上工作.
原因是什么?
这是内核
__kernel void matrixVectorMul(__global float* resultVector,
__global float* matrixA,
__global float* vectorB,
int width_A)
{
int tx = get_global_id(0);
float value = 0;
for (unsigned int k = 0; k < width_A; ++k) {
value += matrixA[tx * width_A + k] * vectorB[k];
}
resultVector[tx] = value;
}
Run Code Online (Sandbox Code Playgroud)
和主机代码
#include <stdlib.h>
#define __CL_ENABLE_EXCEPTIONS
#include "cl.hpp"
#include <fstream>
#include <iostream>
#include <time.h>
#include <cmath>
#define LOCAL_SIZE 512
#define WIDTH_A (4096*2)
#define HEIGHT_A (4096*2)
float *matrix_A; …Run Code Online (Sandbox Code Playgroud)