c - Template function to print a Thrust vector -
i'm writing matrix template class prints both file , std::cout, i.e.:
matrix<float> mymat; ... mymat.cout(...) // print std::cout mymat.write("out.txt") // print file both share common underlying printing function i'm trying implement template too, since i've seen different examples use thrust::copy write data both std::cout , files.
below skeleton of i've done, is outputting garbage. point errors may have made? example, allowed pass std::cout around this?
template <typename data_t> matrix { ... template <typename out_t> int printto(out_t &out, ...) { data_t *start = ..., *end = ...; ... thrust::copy(start, end, std::ostream_iterator<data_t>(out, " ")); ... } int cout(...) { ... printto(std::cout, ...); ... } int write(char* path, ...) { ... std::ofstream file; file.open(path); printto(file, ...); ... } } edit:
- changing
int printto(std::ostream &out, ...) {...}not fix problem. - more info: read data matrix
thrust::device_vector<t>,dvec, , convertdata_tpointerpvecusingthrust::raw_pointer_cast(&dvec[0])(as cublas library uses raw pointers). operate onpvec, want print out. - i've tried printing pointer of original
thrust::device_vectordirectly (i.e.*dvec) , does work:thrust::copy((*dvec).begin(), (*dvec).begin() + n ...). why can copy using*dveciterators , not raw pointer castpvec?
don't use raw_pointer_cast here. fool thrust thinking you've got pointer data on host, why code isn't giving expect. have expected code crash.
to copy device_vector ostream_iterator, use thrust::copy directly:
thrust::device_vector<float> vec = ... thrust::copy(vec.begin(), vec.end(), std::ostream_iterator<float>(std::cout, " "));
Comments
Post a Comment