我的程序首先从HDD(具有c ++file.read
功能)读取2张图像,然后在GPU和CPU(与简单的CPU计算混合在一起的一堆CUDA内核)上执行这些图像的计算。我有大约2000对图像要处理。读取和计算时间几乎相等。是否有相对简单的读取和处理并行化实现?
我知道,使用CUDA流,我可以相对于主机(CPU)异步评估内核,但是在这里,我的计算既复杂又复杂。那么,是否有可能使用某种CPU多线程?
我想要的是:
readfromHDD(im-1);
readfromHDD(im-2);
for(int i=3;i<1998;i=i+2){
readfromHDD(im-i); | functions inside the
readfromHDD(im-(i+1)); | for loop are evaluated
ProcessGPU&CPU(im-(i-2),im-(i-1)); | concurrently
Synchronize_Reading_and_processing;
end
我认为,无需发布我的实际代码。我以前从未做过多线程,所以我不知道它如何与CUDA内核一起使用。任何提示表示赞赏。
谢谢
我非常偏爱pthread,并在读取器的顶部实现异步包装器,该包装器在您请求下一组数据时进行同步。
这是我能想到的最简单的方法。我提供了一些应该易于编译并充分演示实现的内容。祝你好运。
main.cpp演示了用法。
#include "Reader.h"
#include "Reader_Async_Wrapper.h"
using namespace std;
int main() {
Reader *reader = new Reader("test");
Reader_Async_Wrapper async_reader(reader);
int img_index=0;
char* data;
data = async_reader.get_data();
while(((int*)data)[0]!=-1){
cout<<"processing image "<<img_index<<endl;
sleep(2);
cout<<"processed image "<<img_index++<<endl;
delete[] data;
data = async_reader.get_data();
}
return 0;
}
Reader.h是一个简单的串行实现的文件I / O类
#include <iostream>
#include <fstream>
#include <unistd.h>
using namespace std;
class Reader{
public:
bool isFinished(){return finished;}
Reader(string file_name){
open_file(file_name);
finished=false;
img_index=0;
}
char* read_data(){
cout<<"Reading img: "<<img_index<<endl;
sleep(1);
cout<<"Read img: "<<img_index++<<endl;
if(img_index==10)finished=true;
return new char[1000];
}
private:
bool finished;
int img_index;
void open_file(string name){
// TODO
}
};
Reader_Async_Wrapper.h是Reader.h的简单包装,使其可以异步运行
#include "Reader.h"
#include <pthread.h>
using namespace std;
class Reader_Async_Wrapper{
public:
pthread_t thread;
pthread_attr_t attr;
Reader* reader;
pthread_barrier_t barrier;
Reader_Async_Wrapper(Reader* reader):reader(reader){
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_barrier_init (&barrier, NULL, 2);
pthread_create(&thread, &attr, &Reader_Async_Wrapper::threadHelper, this);
finished=false;
image_data=NULL;
}
void finish(){
pthread_attr_destroy(&attr);
void *status;
pthread_join(thread, &status);
}
char* get_data(){
pthread_barrier_wait (&barrier);
return image_data;
}
void clear_buffer(char* old_image){
delete[] old_image;
}
private:
char* image_data;
static void *threadHelper(void * contx){
return ((Reader_Async_Wrapper *)contx)->async_loop();
}
bool finished;
void *async_loop(){
while(!finished){
if(reader->isFinished()){
finished=true;
image_data=new char[sizeof(int)];
((int*)image_data)[0]=-1;
}else
image_data=reader->read_data();
pthread_barrier_wait(&barrier);
}
pthread_exit(NULL);
return NULL;
}
};
我建议改善与检测文件结尾关联的处理(假设您正在从单个长文件中读取)。否则,我认为您可以轻松地将此扩展到您的应用程序。
只要您不打算同时处理许多案件,并且主要将其用作隐藏与读取文件相关的延迟的方法,则此方法应该足够。
如果要同时处理许多情况,则可以使用包装器包装文件的读取和处理。关于CUDA,我相信他们都应该共享CUDA环境。
如果您希望能够在GPU上并行处理,那么我建议您做一些事情:创建包装类的多个副本,为您想要的每个并行实例创建一个副本。为类构造函数中的每个异步实例分配一次足够的内存。为每个线程指定一个GPU线程,以便内核可以并行运行。在GPU线程上执行所有内存复制和内核执行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句