Using thrust::copy() to copy from a file to a device_vector

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.

I don’t think you can read directly from a file to a device vector.
To test this,you can try copying from the file to a host_vector and then from the host_vector to a device_vector.

Thank you for your reply.

As you said, if I read a file into a host_vector and then copy the host_vector to a device_vector like the code below, the result is correct.

#include <fstream>

#include <iostream>

#include <thrust/host_vector.h>

#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::host_vector<unsigned> H(num);

  istream_iterator<unsigned> beg(ifile), end;

  thrust::copy(beg, end, H.begin());

thrust::device_vector<unsigned> D(num);

  thrust::copy(H.begin(), H.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;

}

However, in the code above, I copy the contents of device_vector directly into an output file and it succeeds.

So, I think it is a kind of a bug in thrust, not being able to copy the contents of a file into a device_vector directly as in the original code (the code in the first question).

You can read from a device vector in thrust because thrust does an implicit memcopy operation to a temporary host vector and then to the file.
You cannot access files from a gpu and you cannot directly access gpu memory as far as I know