     string host = buf;
     host = host.substr(0, host.size() - 1);
     strcpy(buf, host.c_str());
-
-    int devCount;
-    int deviceNum=-1;
-    CUDA_CHECK(cudaGetDeviceCount(&devCount));
-
     if (commRank == 0)
     {
         map<string, vector<int> > hosts;
         map<string, int> devCounts;
+        hosts[buf].push_back(0);
+        devCounts[buf] = devCount;
+
         MPI_Status stat;
         MPI_Request req;
-
-        hosts[buf].push_back(0);
-        devCounts[buf] = devCount;
         for (int i = 1; i < commSize; ++i)
         {
             MPI_Recv(buf, 1024, MPI_CHAR, i, 0, MPI_COMM_WORLD, &stat);

#undef _GLIBCXX_USE_INT128

#include <iostream>
#include <vector>
#include <cublas_v2.h>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>

 * @param commSize - the size of MPI comm world
 */

/// Note that this function was lifted from http://code.google.com/p/gpmr/
void setDevice(int commRank, int commSize)
{
    int devCount;
    int deviceNum=0;
    CUDA_CHECK(cudaGetDeviceCount(&devCount));
#ifdef HAVE_MPI  
    FILE * fp = popen("/bin/hostname", "r");
    char buf[1024];
    if (fgets(buf, 1023, fp) == NULL) strcpy(buf, "localhost");
