主要是之前老师让不同的方法实现计算自然对数,了解不同并行语言的特点。所以在用了多线程,openMP后,想用opencL实现以下,先介绍一下算法
方法一.
代码主机端
/*
项目:openCL的矩阵相乘
作者:刘荣
时间:2012.11.20
*/
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//kernel函数
std::string
convertToString(const char *filename)//将kernel源码,即自己写的并行化的函数,转化成字符串
{
size_t size;
char* str;
std::string s;
std::fstream f(filename, (std::fstream::in | std::fstream::binary));
if(f.is_open())
{
size_t fileSize;
f.seekg(0, std::fstream::end);
size = fileSize = (size_t)f.tellg();
f.seekg(0, std::fstream::beg);
str = new char[size+1];
if(!str)
{
f.close();
std::cout << "Memory allocation failed";
return NULL;
}
f.read(str, fileSize);
f.close();
str[size] = '\0';
s = str;
delete[] str;
return s;
}
else
{
std::cout << "\nFile containg the kernel code(\".cl\") not found. Please copy the required file in the folder containg the executable.\n";
exit(1);
}
return NULL;
}
int main()
{
//
double start,end,time1,time2;
//查询平台
cl_int ciErrNum;
cl_platform_id platform;
ciErrNum = clGetPlatformIDs(1, &platform, NULL);
if(ciErrNum != CL_SUCCESS)
{
cout<<"获取设备失败"<
kernel函数
// Enter your kernel in this window
__kernel
void CaluE(__global double* result,
int StepNum,
int MaxItem
)
{
int id=get_global_id(0);
double start,end,res;
int offest = StepNum/MaxItem;
//获得所求的初末
start = id+1;
end = id+offest;
//开始计算
res = 0;
double fact = 1;
for(int i = start; i < end; i++)
{
fact *= i;
res += (1.0/fact);
}
//传回
result[id*2] = res;
result[id*2+1] = fact;
barrier(CLK_LOCAL_MEM_FENCE);
};
方法二
主机端程序
/*
项目:openCL的矩阵相乘
作者:刘荣
时间:2012.11.20
*/
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//kernel函数
std::string
convertToString(const char *filename)//将kernel源码,即自己写的并行化的函数,转化成字符串
{
size_t size;
char* str;
std::string s;
std::fstream f(filename, (std::fstream::in | std::fstream::binary));
if(f.is_open())
{
size_t fileSize;
f.seekg(0, std::fstream::end);
size = fileSize = (size_t)f.tellg();
f.seekg(0, std::fstream::beg);
str = new char[size+1];
if(!str)
{
f.close();
std::cout << "Memory allocation failed";
return NULL;
}
f.read(str, fileSize);
f.close();
str[size] = '\0';
s = str;
delete[] str;
return s;
}
else
{
std::cout << "\nFile containg the kernel code(\".cl\") not found. Please copy the required file in the folder containg the executable.\n";
exit(1);
}
return NULL;
}
int main()
{
//
double start,end,time1,time2;
//查询平台
cl_int ciErrNum;
cl_platform_id platform;
ciErrNum = clGetPlatformIDs(1, &platform, NULL);
if(ciErrNum != CL_SUCCESS)
{
cout<<"获取设备失败"<
kernel函数
// Enter your kernel in this window
__kernel
void CaluE(__global double* result,
int StepNum,
int MaxItem
)
{
int id = get_global_id(0);
float fact = 1;
double e = 0;
for(int i = id+1; i <= StepNum;i+=MaxItem)
{
for(int j=0; j
// Enter your kernel in this window
__kernel
void CaluE(__global double* result,
int StepNum,
int MaxItem
)
{
int id=get_global_id(0);
double start,end,res;
int offest = StepNum/MaxItem;
//获得所求的初末
start = id+1;
end = id+offest;
//开始计算
res = 0;
double fact = 1;
for(int i = start; i < end; i++)
{
fact *= i;
res += (1.0/fact);
}
//传回
result[id*2] = res;
result[id*2+1] = fact;
barrier(CLK_LOCAL_MEM_FENCE);
};