将简单的python代码转换为pyopencl

时间:2015-01-19 23:38:16

标签: python

我正在尝试将我的R9 280 GPU与pyopencl一起使用,但我无法让它工作,因为我的python和pyopencl知识有点干。我想知道有谁帮助我或者至少指引我走向正确的方向。下面简单的python脚本将upload.txt读入内存,并在使用基本乘法函数后尝试匹配随机创建的数字。基本上我不能为此编写内核。如你所见,它只是为gpu打开工作,需要内核来处理文件,检查随机创建的数字并将匹配写入文件。提前谢谢。

#! /usr/bin/env python
import random
import pyopencl as cl
from os import environ, system
def Multiply(x,y):
   return 4*y/x

environ["PYOPENCL_CTX"]="0"
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)

run = True  
target = open("upload.txt", 'rb')
readit = target.read()
while run: 
r = 8;
p = random.randrange(1,5000);
q = Multiply(r,p);
z = str(q);
print z;
if z in readit:
    file = open('found.txt', 'ab')
    file.write(str(z))
    file.close()
    print "Found ",z
    run = False

1 个答案:

答案 0 :(得分:1)

我没有评论权限,但请参阅以下内容

import random
import pyopencl as cl
from os import environ, system

def Multiply(x,y):
   return 4*y/x

environ["PYOPENCL_CTX"]="0"
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)

# run = True  -- no need for this
with open("upload.txt", 'rb') as target:        # pythonic & safe way of opening files
    readit = target.read()

while True: 
    r = 8;
    p = random.randrange(1,5000);
    q = Multiply(r,p);
    z = str(q);
    print z;
    if z in readit:
        with open('found.txt', 'ab') as File:  # can't use file as variable name, hence the caps
            File.write(z)
            print("Found ", z)
        break

在你明确问题之前我不能帮助你 - 请详细说明你得到的错误/故障&你期望什么输出

编辑:因为您正在处理图形处理&东西,数据源文件可能会变得非常大 - 我建议使用延迟(缓冲)读取,请参阅this以获得非常简单的实现