0

I am fairly new to Cuda C, and I have a problem exporting a large 2D array in Cuda C using ofstream. The code works fine, but the performance is not really what I expected. Is there any other faster method other than fstream? I've tried using ostringstream but it doesn't really show any improvements.

HANDLE_ERROR( cudaMemcpy( sseismo, seismo,sizeof(float)*(DIMX*samp),cudaMemcpyDeviceToHost ) );
sprintf(nmfile, "seismo%ix%itau%08ivz.txt",4000,4000,1 );
std::ofstream outseis(nmfile); // output, normal file
for (int jj=0; jj<4000; jj++) 
{
    for (int ii=0; ii<4000; ii++) 
    {
    int ij=(DIMX)*jj + ii;           
    outseis<<sseismo[ij]<<" ";   
    }
    outseis<<"\n";
}
outseis.close();
Leon
  • 69
  • 2
  • 11
  • 4
    In "CUDA C" the host code is passed off to the host compiler. This question has nothing to do with CUDA. – Robert Crovella Aug 21 '13 at 17:38
  • If you decide to use binary format as Eric suggested, check [this](http://stackoverflow.com/questions/11563963/writing-a-binary-file-in-c-very-fast) as well. – stuhlo Aug 21 '13 at 17:52

2 Answers2

2

You could output the data in binary format rather than text format.

outseis.write((char*)sseismo[jj*(DIMX)], 4000*sizeof(sseismo[0]));

Otherwise outseis<< is almost the fastest text output you could get.

Please refer to here for more info about std::ofstream::write

http://en.cppreference.com/w/cpp/io/basic_ostream/write

kangshiyin
  • 9,681
  • 1
  • 17
  • 29
  • well, thank you.. turns out it much more efficient using binary format as an output file.. – Leon Aug 26 '13 at 08:02
0

If writing binary data isn't an option, you can squeeze a few hundred milliseconds by writing the inner loop to a stringstream and writing that to the file (I get 1.317ms instead of ~1.700ms).

#include <fstream>
#include <sstream>
int main()
{
    const char nmfile[] = "out.txt";
    std::ofstream outseis(nmfile); // output, normal file

    for (int jj=0; jj<4000; jj++)
    {
            std::stringstream buf;
            for (int ii=0; ii<4000; ii++)
            {
                    int ij = jj + ii;
                    buf<<ij<<" ";
            }
            outseis << buf.str() << "\n";

    }
    outseis.close();
}
Radu Chivu
  • 1,057
  • 7
  • 17