【问题标题】:How can I do other oprations on CPU while kenrels in GPU are running?当 GPU 中的内核正在运行时,如何在 CPU 上执行其他操作?
【发布时间】:2021-12-29 22:49:58
【问题描述】:

内核需要几分钟才能完成。 我想在 GPU 中的内核正在处理时在控制台中显示一个移动条。

正常情况下,clEnqueueNDRangeKernel这个函数会执行内核,当它们完成后,CPU会继续执行下面的操作,如clWaitForEventsclReleaseMemObject等。

但是,我希望 CPU 在 clEnqueueNDRangeKernel 之后但在内核完成之前继续打印处理条。

有什么办法吗?

【问题讨论】:

    标签: opencl


    【解决方案1】:

    创建一个处理 GPU 队列的线程和一个处理控制台输出的线程。 您可以通过分配全局变量在两个线程之间共享信息。

    #include <iostream>;
    #include <thread>
    using namespace std;
    
    volatile bool not_finished = true;
    float progress = 0.0f;
    
    void do_console_output() {
        while(not_finished) {
            // do console output ...
            cout << progress << endl;
        }
    }
    void do_opencl_stuff() {
        while(not_finished) {
            // do OpenCL stuff ...
            progress += 0.01f;
        }
    }
    
    int main() {
        thread console_thread(do_console_output); // launch a separate thread
        do_opencl_stuff(); // execute this in the main thread
        console_thread.join();
        return 0;
    }
    

    【讨论】:

    • 谢谢,这正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多