【问题标题】:Convert simple python code to pyopencl将简单的python代码转换为pyopencl
【发布时间】:2015-01-23 11:09:03
【问题描述】:

我正在尝试将我的 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

【问题讨论】:

    标签: python


    【解决方案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 以获得非常简单的实现

    【讨论】:

    • 基本上我无法为此编写内核。如您所见,它只是为 gpu 打开作业,需要内核来读取文件、检查随机创建的数字并将匹配项写入文件。
    • @ico 谢谢,但请将您的评论复制粘贴到问题框中,queueupload.txt 是如何连接的?你是在文件中写了queue还是什么?
    • 队列和upload.txt之间没有连接,因为upload.txt中充满了50、67、590等随机数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 2013-01-19
    • 2015-01-03
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多