gaussblurinc
gaussblurinc

Reputation: 3692

using MPI: C++ std::bad_alloc

i'm working with supercomputer, using MPI. but problem in.. C++ have a program, which open file with data and read it into vector<long>v1

    //open file
    ...
   vector<long>v1;

   while (!f1.eof()){
      //input data into 
      v1.push_back(s1);
   }

okey, when file of data contains only 50 millions of "long-numbers", it worked perfect. but when file of data contains over 75 millions of "long-numbers", it failed with exception:

   std::bad_alloc();

how to improve this?

besides, use many processors ( over 100 )

Upvotes: 1

Views: 908

Answers (2)

David Schwartz
David Schwartz

Reputation: 182827

Don't use a vector for this. A vector requires all its elements to fit in consecutive memory locations and it isn't suitable for very large collections. The right data structure to use depends on your access patterns, list will work, but it will waste a lot of memory (two pointers for each long you store). Perhaps you want to break the longs into groups of 100 or so and make a linked list of those groups. Again, the right answer depends on your actual outer problem.

Upvotes: 4

John Humphreys
John Humphreys

Reputation: 39324

While I don't have much experience with supercomputers (at all), I can tell you that std::bad_alloc should only occur when you run out of system resources.

Chances are in this case that you have reached the limit the computer is imposing on your heap (either from an operating system perspective or a physical perspective {kind of the same thing in the end}) since your vector will be dynamically allocating elements on the heap.

You can try using top or a similar command to monitor your resource usage, and check your system settings against what you're actually using.

Another note - you should create your vector and call reserve() if you know how many elements it will roughly be using - it will greatly improve your efficiency.

Upvotes: 1

Related Questions