Hi.
I would like to read a list of integer numbers from a file and copy them into a device_vector.
My code is:
#include <fstream>
#include <iostream>
#include <iterator>
#include <thrust/device_vector.h>
using namespace std;
int main() {
ifstream ifile("rands.txt");
int num;
ifile >> num; // number of numbers in `rands.txt'
thrust::device_vector<unsigned> D(num);
istream_iterator<unsigned> beg(ifile), end;
thrust::copy(beg, end, D.begin());
ofstream ofile("rands_out.txt");
thrust::copy(D.begin(),
D.end(),
ostream_iterator<unsigned>(ofile, "\n"));
ifile.close();
ofile.close();
return 0;
}
The input file being read, `rand.txt’ is:
10
1804289383
846930886
1681692777
1714636915
1957747793
424238335
719885386
1649760492
596516649
1189641421
The output file, `rands_out.txt’ is a mere copy of the input file except the first element that represents the size of the list.
The expected result is:
1804289383
846930886
1681692777
1714636915
1957747793
424238335
719885386
1649760492
596516649
1189641421
However, the output file generated is:
1804289383
0
0
0
0
0
26816656
0
0
0
Actually, the output file is undetermined (It changes everytime I run the program).
I know that there are many other approaches for doing this, but my question is, why this method doesn’t work?
I am using:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2011 NVIDIA Corporation
Built on Thu_Jan_12_14:41:45_PST_2012
Cuda compilation tools, release 4.1, V0.2.1221
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-51)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Thank you.
brillouin.