所以我试图在Haskell中使用OpenCL
,但它似乎只处理我给它的列表的一半,在每个元素之间放置一个0然后只处理第一个{ {1}}元素,即:列表我给它:n
它看到的列表:[1,2,3,4,5,6]
。我使用[1,0,2,0,3,0]
和OpenCL
' s cabal
AMD
包
OpenCL内核代码(C):
OpenCL library version 2.9-1.
Haskell代码:
int power(int a, int n, int mod)
{
int power = a;
int result = 1;
while (n)
{
if (n & 1)
result = (result * power) % mod;
power = (power * power) % mod;
n >>= 1;
}
return result;
}
bool witness(int n, int s, int d, int a)
{
int x = power(a, d, n);
int y;
while (s) {
y = (x * x) % n;
if (y == 1 && x != 1 && x != n - 1)
return 0;
x = y;
--s;
}
if (y != 1)
return 0;
return 1;
}
int is_prime_mr(int n)
{
if (((!(n & 1)) && n != 2) || (n < 2) || (n % 3 == 0 && n != 3))
return 0;
if (n <= 3)
return 1;
int d = n / 2;
int s = 1;
while (!(d & 1)) {
d /= 2;
++s;
}
if (n < 1373653)
return witness(n, s, d, 2) && witness(n, s, d, 3);
if (n < 9080191)
return witness(n, s, d, 31) && witness(n, s, d, 73);
if (n < 4759123141)
return witness(n, s, d, 2) && witness(n, s, d, 7) && witness(n, s, d, 61);
if (n < 1122004669633)
return witness(n, s, d, 2) && witness(n, s, d, 13) && witness(n, s, d, 23) && witness(n, s, d, 1662803);
if (n < 2152302898747)
return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11);
if (n < 3474749660383)
return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13);
return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13) && witness(n, s, d, 17);
}
__kernel void duparray(__global int *in, __global int *out )
{
int id = get_global_id(0);
if(id == 1)
{
int i = 0;
for(i = 0; i <= 200; i++)
{
printf("%d\t", in[i]);
}
}
//printf("%d :: %d\t\t",id, in[id]*2);
out[id] = (is_prime_mr(in[id]) == 0) ? 0 : in[id];
//out[id] = in[id];
}
对于Haskell代码,我使用Github中第一个示例的修改版本来使用OpenCL包:https://github.com/IFCA/opencl
我还保留了我在OpenCL内核中的痕迹。那两个import Control.Parallel.OpenCL
import Foreign( castPtr, nullPtr, sizeOf )
import Foreign.C.Types( CFloat )
import Foreign.Marshal.Array( newArray, peekArray )
import System.IO
upper = 200
lower = 0
main :: IO ()
main = do
-- Initialize OpenCL
(platform:_) <- clGetPlatformIDs
(dev:_) <- clGetDeviceIDs platform CL_DEVICE_TYPE_ALL
context <- clCreateContext [CL_CONTEXT_PLATFORM platform] [dev] print
q <- clCreateCommandQueue context dev []
handle <- openFile "gpuPrimalityTest.c" ReadMode
programSource <- hGetContents handle
-- Initialize Kernel
program <- clCreateProgramWithSource context (programSource)
clBuildProgram program [dev] ""
kernel <- clCreateKernel program "duparray"
-- Initialize parameters
let original = [lower .. upper] :: [Int]
elemSize = sizeOf (0 :: Int)
vecSize = elemSize * length original
-- putStrLn $ "Original array = " ++ show original
input <- newArray original
mem_in <- clCreateBuffer context [CL_MEM_READ_ONLY, CL_MEM_COPY_HOST_PTR] (vecSize, castPtr input)
mem_out <- clCreateBuffer context [CL_MEM_WRITE_ONLY] (vecSize, nullPtr)
clSetKernelArgSto kernel 0 mem_in
clSetKernelArgSto kernel 1 mem_out
-- Execute Kernel
eventExec <- clEnqueueNDRangeKernel q kernel [length original] [] []
-- Get Result
eventRead <- clEnqueueReadBuffer q mem_out True 0 vecSize (castPtr input) [eventExec]
result <- peekArray (length original) input
putStrLn $ "Result array = " ++ show (filter (\x -> x/=0) result)
return ()
是我试图调试它的方式。
感谢任何帮助
编辑:当我使用printf
位整数
答案 0 :(得分:2)
您需要确保主机代码中缓冲区的数据布局与设备所需的匹配。您可以在示例代码中使用postController
代替Int32
来实现此目的。
答案 1 :(得分:0)
使用long
对我有用:
__kernel void duparray(__global long *in, __global long *out )