// This program demonstrates how to find an element in a vector // using the CUDA standard algorithms in Taskflow. #include #include int main(int argc, char* argv[]) { if(argc != 2) { std::cerr << "usage: ./cuda_find N\n"; std::exit(EXIT_FAILURE); } unsigned N = std::atoi(argv[1]); // gpu data auto gdata = tf::cuda_malloc_shared(N); auto gfind = tf::cuda_malloc_shared(1); // cpu data auto hdata = std::vector(N); size_t tgpu{0}, tcpu{0}; // initialize the data for(unsigned i=0; i(end-beg).count(); // -------------------------------------------------------------------------- // CPU find // -------------------------------------------------------------------------- beg = std::chrono::steady_clock::now(); auto hiter = std::find_if( hdata.begin(), hdata.end(), [=](int v) { return v == 100; } ); end = std::chrono::steady_clock::now(); tcpu += std::chrono::duration_cast(end-beg).count(); // -------------------------------------------------------------------------- // verify the result // -------------------------------------------------------------------------- if(unsigned hfind = std::distance(hdata.begin(), hiter); *gfind != hfind) { printf("gdata[%u]=%d, hdata[%u]=%d\n", *gfind, gdata[*gfind], hfind, hdata[hfind] ); throw std::runtime_error("incorrect result"); } // output the time std::cout << "GPU time: " << tgpu << '\n' << "CPU time: " << tcpu << std::endl; // delete the memory tf::cuda_free(gdata); tf::cuda_free(gfind); return 0; }