Exemplo n.º 1
0
void NetworkPlugin::sendMemoryUsage() {
	pbnetwork::Stats stats;

	stats.set_init_res(m_init_res);
	double res = 0;
	double shared = 0;
#ifndef WIN32
	process_mem_usage(shared, res);
#endif

	double e_res;
	double e_shared;
	handleMemoryUsage(e_res, e_shared);

	stats.set_res(res + e_res);
	stats.set_shared(shared + e_shared);
	stats.set_id(stringOf(getpid()));

	std::string message;
	stats.SerializeToString(&message);

	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_STATS);

	send(message);
}
Exemplo n.º 2
0
NetworkPlugin::NetworkPlugin() {
	m_pingReceived = false;

	double shared;
#ifndef WIN32
	process_mem_usage(shared, m_init_res);
#endif
}
Exemplo n.º 3
0
int main (int argc, char**argv) {
    if (argc < 3) {
	printf ("Usage:\n\nmih <infile> <outfile> [options]\n\n");
	printf ("Options:\n");
	printf (" -nMs <n1 n2 n3 ...>  Set an array of multiples of 'one million items' to experiment with\n");
	printf (" -nM <number>         Set the index from the nMs array to be used for this run (1 is the first)\n");
	printf (" -Q <number>          Set the number of query points to use from <infile>, default all\n");
	printf (" -B <number>          Set the number of bits per code, default autodetect\n");
	printf (" -m <number>          Set the number of chunks to use, default 1\n");
	printf ("\n");
	return 0;
    }

    char *infile = argv[1];
    char *outfile = argv[2];
		
    UINT32 N = 0;
    int B = 0;
    int m = 1;
    UINT32 K = 100;
    int nM = 0;
    int NQ = 0;
    double *nMs = NULL;
    int nnMs = 0;
	
    for (int argnum = 3; argnum < argc; argnum++) {
	if (argv[argnum][0] == '-') {
	    switch (argv[argnum][1]) {
	    case 'B':
		B = atoi(argv[++argnum]);
		break;
	    case 'K':
		K = atoi(argv[++argnum]);
		break;
	    case 'n':
		if (!strcmp(argv[argnum], "-nMs")) {
		    nMs = new double[100];
		    while (++argnum < argc)
			if (argv[argnum][0] != '-') {
			    nMs[nnMs++] = atof(argv[argnum]);
			} else {
			    argnum--;
			    break;
			}
		} else if (!strcmp(argv[argnum], "-nM")) {
		    nM = atoi(argv[++argnum]);
		}
		break;
	    case 'Q':
		NQ = atoi(argv[++argnum]);
		break;
	    case 'm':
		m = atoi(argv[++argnum]);
		break;
	    default: 
		printf ("Unrecognized Option or Missing Parameter when parsing: %s\n", argv[argnum]);
		return EXIT_FAILURE;
	    }
	} else {
	    printf ("Invalid Argument: %s\n", argv[argnum]);
	    return EXIT_FAILURE;
	}
    }

    MATFile *ifp = NULL;
    mxArray *mxnMs = NULL;
    mxArray *mxret = NULL;
    /* Opening output file to read "ret" and "nMs" if available */
    ifp = matOpen(outfile, "r");
    if (ifp) {
	mxnMs = matGetVariable(ifp, "nMs");
	mxret = matGetVariable(ifp, "ret");

	double *nMs2 = (double*)mxGetPr(mxnMs);
	int nnMs2 = mxGetN(mxnMs);

	if (nMs != NULL) {
	    if (nnMs != nnMs2) {
		printf("#nMs is different from the #nMs read from the output file %d vs. %d.\n", nnMs, nnMs2);
		return EXIT_FAILURE;
	    }
	    for (int i=0; i<nnMs; i++)
		if (int(nMs[i]*1.0e6) !=  int(nMs2[i] * 1.0e6)) {
		    printf("nMs are different from the nMs read from the output file.\n");
		    return EXIT_FAILURE;
		}
	    delete[] nMs;
	}

	nnMs = nnMs2;
	nMs = nMs2;
	matClose (ifp);
    } else {
	mxnMs = mxCreateNumericMatrix(1, nnMs, mxDOUBLE_CLASS, mxREAL);
	double *nMs2 = (double*)mxGetPr(mxnMs);
	for (int i=0; i<nnMs; i++)
	    nMs2[i] = nMs[i];
	delete[] nMs;
	nMs = nMs2;
    }

    if (mxret == NULL) {
	const char* ab[] = {"res", "nres", "stat", "wt", "cput", "vm", "rss", "m"};
	mxret = mxCreateStructMatrix(1000, nnMs, 8, ab);
    }
    /* Done with initializing mxnMs and mxret and sanity checks */

    /* Loading the codes and queries from the input file */	
    ifp = matOpen (infile, "r");
    if (!ifp) {
	printf ("Failed to open input file. Aborting.\n");
	return EXIT_FAILURE;
    }

    printf ("Loading codes... ");
    fflush (stdout);
    mxArray *mxcodes = matGetVariable (ifp, "B");
    printf ("done.\n");
		
    printf ("Loading queries... ");
    fflush (stdout);
    mxArray *mxqueries = matGetVariable (ifp, "Q");
    printf ("done.\n");
    matClose (ifp);
    /* Done with the inputs */
	
    int dim1codes = mxGetM(mxcodes);
    int dim1queries = mxGetM(mxqueries);
    if (!B)
	B = mxGetM(mxcodes)*8;
    if (B % 8 != 0) {
	printf ("Non-multiple of 8 code lengths are not currently supported.\n");
	return EXIT_FAILURE;
    }
    N = 1.0e6 * nMs[nM-1];
    if (N)
	N = std::min ( (UINT32)N, (UINT32)mxGetN(mxcodes) );
    if (NQ > 0)
	NQ = std::min( NQ, (int)mxGetN(mxqueries) );
    else
	NQ = mxGetN (mxqueries);
	
    printf("nM = %d |", nM);
    printf(" N = %.0e |", (double)N);
    printf(" NQ = %d |", NQ);
    printf(" B = %d |", B);
    printf(" m = %d", m);
    printf("\n");
		
    /* Run multi-index hashing for 1,10,100,1000 NN and store the required stats */
    int mold = -1;
    mihasher* MIH = NULL;
    clock_t start0, end0;
    time_t start1, end1;
    qstat *stats = (qstat*) new qstat[NQ];

    for (K = 1; K<=1000; K *= 10) {
	mxArray *mxresults = mxCreateNumericMatrix (K, NQ, mxUINT32_CLASS, mxREAL);
	mxArray *mxnumres = mxCreateNumericMatrix (B+1, NQ, mxUINT32_CLASS, mxREAL);
	mxArray *mxctime = mxCreateNumericMatrix (1, 1, mxDOUBLE_CLASS, mxREAL);
	mxArray *mxwtime = mxCreateNumericMatrix (1, 1, mxDOUBLE_CLASS, mxREAL);
	mxArray *mxvm  = mxCreateNumericMatrix (1, 1, mxDOUBLE_CLASS, mxREAL);
	mxArray *mxrss = mxCreateNumericMatrix (1, 1, mxDOUBLE_CLASS, mxREAL);
	mxArray *mxstats = mxCreateNumericMatrix (6, NQ, mxDOUBLE_CLASS, mxREAL);

	UINT32 *numres = (UINT32*) mxGetPr (mxnumres);
	UINT32 *results = (UINT32*) mxGetPr (mxresults);
	double *ctime = (double*) mxGetPr (mxctime);
	double *wtime = (double*) mxGetPr (mxwtime);	    	    
	double *vm  = (double*) mxGetPr (mxvm);
	double *rss = (double*) mxGetPr (mxrss);	    	    
	double *stats_d = (double*) mxGetPr (mxstats);
	    
	/* if m changes from K to K */
	if (mold != m) {
	    if (MIH != NULL) {
		printf ("Clearing up... ");
		fflush (stdout);
		delete MIH;
		printf ("done.          \r");
		fflush (stdout);
	    }

	    MIH = new mihasher(B, m);
		
	    printf ("Populating %d hashtables with %.0e entries...\n", m, (double)N);
	    fflush (stdout);
	    start1 = time(NULL);
	    start0 = clock();
	    
	    MIH->populate ( (UINT8*) mxGetPr(mxcodes), N, dim1codes);
	    
	    end0 = clock();
	    end1 = time(NULL);

	    double ct = (double)(end0-start0) / (CLOCKS_PER_SEC);
	    double wt = (double)(end1-start1);

	    printf ("done. | cpu %.0fm%.0fs | wall %.0fm%.0fs\n", ct/60, ct-60*int(ct/60), wt/60, wt-60*int(wt/60));
	    // printf ("done.                                            \r");
	    // fflush (stdout);
	}

	printf(" m = %2d |", m);
	printf(" K = %4d |", K);
	printf(" ");
	MIH->setK(K);

	printf("query... ");
	fflush (stdout);
	    
	start1 = time(NULL);
	start0 = clock();
	    
	MIH->batchquery (results, numres, stats, (UINT8*) mxGetPr(mxqueries), NQ, dim1queries);
	    
	end0 = clock();
	end1 = time(NULL);

	*ctime = (double)(end0-start0) / (CLOCKS_PER_SEC) / NQ;
	*wtime = (double)(end1-start1) / NQ;
	process_mem_usage(vm, rss);
	*vm  /= double(1024*1024);
	*rss /= double(1024*1024);
	printf ("done | cpu %.3fs | wall %.3fs | VM %.1fgb | RSS %.1fgb     \n", *ctime, *wtime, *vm, *rss);

	int ind = 1000*(nM-1) + K-1;

	double *pstats_d = stats_d;
	for (int i=0; i<NQ; i++) {
	    pstats_d[0] = stats[i].numres;
	    pstats_d[1] = stats[i].numcand;
	    pstats_d[2] = stats[i].numdups;
	    pstats_d[3] = stats[i].numlookups;
	    pstats_d[4] = stats[i].maxrho;
	    pstats_d[5] = (double) stats[i].ticks / CLOCKS_PER_SEC;

	    pstats_d += 6;
	}

	mxSetFieldByNumber(mxret, ind, 0, mxresults);
	mxSetFieldByNumber(mxret, ind, 1, mxnumres);
	mxSetFieldByNumber(mxret, ind, 2, mxstats);
	mxSetFieldByNumber(mxret, ind, 3, mxwtime);
	mxSetFieldByNumber(mxret, ind, 4, mxctime);
	mxSetFieldByNumber(mxret, ind, 5, mxvm);
	mxSetFieldByNumber(mxret, ind, 6, mxrss);
	mxSetFieldByNumber(mxret, ind, 7, mxCreateDoubleScalar(m));

	mold = m;
    }
    printf ("Clearing up... ");
    fflush (stdout);
    delete MIH;
    printf ("done.          \r");
    fflush (stdout);
    /* Done with mulit-index hashing and storing the stats */
	
    /* Opening the output file for writing the results */
    MATFile *ofp = matOpen (outfile, "w");
    if (!ofp) {
	printf ("Failed to create/open output file. Aborting.\n");
	return EXIT_FAILURE;
    }
			
    printf("Writing results to file %s... ", outfile);
    fflush(stdout);
    matPutVariable(ofp, "nMs", mxnMs);
    matPutVariable(ofp, "ret", mxret);
    printf("done.\n");
    matClose(ofp);
    /* Done with the output file */

    delete[] stats;
    mxDestroyArray (mxnMs);
    mxDestroyArray (mxret);
    mxDestroyArray (mxcodes);
    mxDestroyArray (mxqueries);
    /* skip deleting nMs if it is initialized by new double[] */

    return 0;
}
Exemplo n.º 4
0
int
main (int argc, char *argv[])
{
  gmp_randstate_t rs;
  mpz_t x, y, z, res;
  mpz_ptr xptr, yptr;
  unsigned long int m, n, i, niter, t0, ti;
  double t;   
  uIDD x2, y2, z2;

  gmp_randinit_default (rs);

  mpz_init (x);
  mpz_init (y);
  mpz_init (z);
  mpz_init (res);  

  if (argc == 2)
    {
      m = atoi (argv[1]);
      n = m;
      mpz_urandomb (x, rs, m);
      xptr = x;
      yptr = x;
      x2 = x;
      y2 = x;
      mpz_set(y, x);
    }
  else if (argc == 3)
    {
      m = atoi (argv[1]);
      n = atoi (argv[2]);
      mpz_urandomb (x, rs, m);
      mpz_urandomb (y, rs, n);
      xptr = x;
      yptr = y;
      x2 = x;
      y2 = y;
    }
  else
    {
      fprintf (stderr, "usage: %s m n\n", argv[0]);
      fprintf (stderr, "  where m and n are number of bits in numbers tested\n");
      return -1;
    }

  double vm1, res1;
  process_mem_usage(vm1,res1);
 
  double add_per_sec_idd;   
  printf ("Calibrating CPU speed...");  fflush (stdout);
  TIME (t, z2 = x2+y2);
  printf ("done\n");
  niter = 1 + (unsigned long) (1e4 / t);

  IDDContent::clean_Ht();
  if (argc == 2)
    printf ("Doubling a %lu-bit number %lu times with IDD...", m, niter);
  else
    printf ("Adding %lu-bit number with %lu-bit number %lu times with IDD...", m, n, niter);
  fflush (stdout);
    
  reset_idd_stats();    
  t0 = cputime ();
  for (i = niter; i > 0; i--)
    z2 = x2+y2;    
  ti = cputime () - t0;
  printf ("done!\n");
  #ifdef MONITORING  
  reset_idd_stats();
  z2 = x2+y2;          
  printf("Before cleaning:\n");
  IDDContent::Ht.show_stats();  
  IDDContent::clean_Ht();
  printf("After cleaning:\n");
  IDDContent::Ht.show_stats();    
  show_idd_stats();
  #endif
  add_per_sec_idd = 1000.0 * niter / ti;
  
  double vm2, res2;
  process_mem_usage(vm2,res2);

  printf ("RESULTS:\n");
  printf("x density: %f\n", (double)x2.a->density());
  printf("y density: %f\n", (double)y2.a->density());
  printf("IDD: %f additions per second\n", add_per_sec_idd);
  printf("Memory used: %dKo\n\n", (int)(res2-res1));
  #ifndef HIDE_RESULTS  
  printf("x = "); mpz_out_str(stdout, 10, x);
  printf("\ny = "); mpz_out_str(stdout, 10, y);  
  printf("\nx+y = "); z2.a->print();
  printf("\n");  
  #endif
  mpz_add(z,x,y);
  mpz_t comp;
  mpz_init(comp);
  z2.to_mpz(comp);
  if(mpz_cmp(comp,z)!=0) printf("Result is false!\n");  
  return 0;
}
Exemplo n.º 5
0
void AdminInterface::handleMessageReceived(Swift::Message::ref message) {
	if (!message->getTo().getNode().empty())
		return;

	if (message->getFrom().toBare().toString() != CONFIG_STRING(m_component->getConfig(), "service.admin_jid")) {
		LOG4CXX_WARN(logger, "Message not from admin user, but from " << message->getFrom().toBare().toString());
		return;
	}

	// Ignore empty messages
	if (message->getBody().empty()) {
		return;
	}

	LOG4CXX_INFO(logger, "Message from admin received");
	message->setTo(message->getFrom());
	message->setFrom(m_component->getJID());

	if (message->getBody() == "status") {
		int users = m_userManager->getUserCount();
		int backends = m_server->getBackendCount();
		message->setBody("Running (" + boost::lexical_cast<std::string>(users) + " users connected using " + boost::lexical_cast<std::string>(backends) + " backends)");
	}
	else if (message->getBody() == "online_users") {
		std::string lst;
		const std::map<std::string, User *> &users = m_userManager->getUsers();
		if (users.size() == 0)
			lst = "0";

		for (std::map<std::string, User *>::const_iterator it = users.begin(); it != users.end(); it ++) {
			lst += (*it).first + "\n";
		}

		message->setBody(lst);
	}
	else if (message->getBody() == "online_users_count") {
		int users = m_userManager->getUserCount();
		message->setBody(boost::lexical_cast<std::string>(users));
	}
	else if (message->getBody() == "reload") {
		bool done = m_component->getConfig()->reload();
		if (done) {
			message->setBody("Config reloaded");
		}
		else {
			message->setBody("Error during config reload");
		}
	}
	else if (message->getBody() == "online_users_per_backend") {
		std::string lst;
		int id = 1;

		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
		for (std::list <NetworkPluginServer::Backend *>::const_iterator b = backends.begin(); b != backends.end(); b++) {
			NetworkPluginServer::Backend *backend = *b;
			lst += "Backend " + boost::lexical_cast<std::string>(id) + " (ID=" + backend->id + ")";
			lst += backend->acceptUsers ? "" : " - not-accepting";
			lst += backend->longRun ? " - long-running" : "";
			lst += ":\n";
			if (backend->users.size() == 0) {
				lst += "   waiting for users\n";
			}
			else {
				time_t now = time(NULL);
				for (std::list<User *>::const_iterator u = backend->users.begin(); u != backend->users.end(); u++) {
					User *user = *u;
					lst += "   " + user->getJID().toBare().toString();
					lst += " - non-active for " + boost::lexical_cast<std::string>(now - user->getLastActivity()) + " seconds";
					lst += "\n";
				}
			}
			id++;
		}

		message->setBody(lst);
	}
	else if (message->getBody().find("has_online_user") == 0) {
		User *user = m_userManager->getUser(getArg(message->getBody()));
		std::cout << getArg(message->getBody()) << "\n";
		message->setBody(boost::lexical_cast<std::string>(user != NULL));
	}
	else if (message->getBody() == "backends_count") {
		int backends = m_server->getBackendCount();
		message->setBody(boost::lexical_cast<std::string>(backends));
	}
	else if (message->getBody() == "res_memory") {
		double shared = 0;
		double rss = 0;
#ifndef WIN32
		process_mem_usage(shared, rss);
#endif

		const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
		BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
			rss += backend->res;
		}

		message->setBody(boost::lexical_cast<std::string>(rss));
	}
Exemplo n.º 6
0
int main(int argc, char** argv)
{

    string path=string(argv[0]);
    unsigned int loc=path.rfind("/");

    string basepath= loc == string::npos ? "./" : path.substr(0,loc+1);
    basepath= osgDB::getRealPath (basepath);
    // use an ArgumentParser object to manage the program arguments.
    osg::ArgumentParser arguments(&argc,argv);

    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
    bool pbufferOnly = !arguments.read("--show");


    unsigned int width=512;
    unsigned int height=512;
    arguments.read("--pbuffer-only",width,height);
    int _tileColumns=0;
    int _tileRows=0;

    osg::BoundingBox totalbb;

    double lat=0,lon=0;
    arguments.read("-lat",lat);
    arguments.read("-lon",lon);
    bool untex= arguments.read("-untex");
    string ext;
    bool nogfx= arguments.read("-nogfx",ext);

    std::vector<picture_cell> cells;
    FILE *fp=fopen(argv[1],"r");
    if(!fp) {
        fprintf(stderr,"Can't open %s\n",argv[1]);
        exit(-1);
    }
    int cnt=0;
    while(!feof(fp)) {
        char fname[1024];
        float minx,maxx,miny,maxy,minz,maxz;
        int row,col;
        int res=fscanf(fp,"%f %f %f %f %f %f %d %d %s\n",&minx,&maxx,&miny,&maxy,&minz,&maxz,&col,&row,fname);
        if(res != 9) {
            fprintf(stderr,"Bad parse\n");
            exit(-1);
        }
        if(cnt==0) {
            totalbb=osg::BoundingBox(minx,miny,minz,maxx,maxy,maxz);
            _tileColumns=col;
            _tileRows=row;

        } else {
            picture_cell cell;
            cell.bbox=osg::BoundingBox(minx,miny,minz,maxx,maxy,maxz);
            cell.col=col;
            cell.row=row;
            if(std::string(fname) != "null")
                cell.name=std::string(argv[2])+"/"+std::string(fname);
            else
                cell.name=std::string(fname);
            cells.push_back(cell);

        }
        cnt++;

    }





    osg::Matrixd view,proj;


    std::stringstream os2;
    os2<< "view.mat";

    std::fstream _file(os2.str().c_str(),std::ios::binary|std::ios::in);
    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
            _file.read(reinterpret_cast<char*>(&(view(i,j))),sizeof(double));
    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
            _file.read(reinterpret_cast<char*>(&(proj(i,j))),sizeof(double));
    _file.close();

    // std::ostringstream os;
    // os <<"subtile.ppm";//<<":deflate";
    vips::VImage raw;
    vips::VImage raw_untex;
    printf("X: %d Y: %d\n",width*_tileColumns,height*_tileRows);
    raw.initdesc(width*_tileColumns,height*_tileRows,3,vips::VImage::FMTUCHAR,vips::VImage::NOCODING,vips::VImage::sRGB,1.0,1.0,0,0);
    if(untex)
        raw_untex.initdesc(width*_tileColumns,height*_tileRows,3,vips::VImage::FMTUCHAR,vips::VImage::NOCODING,vips::VImage::sRGB,1.0,1.0,0,0);
    double vm, rss;
    process_mem_usage(vm, rss);
    cout << "VM: " << get_size_string(vm) << "; RSS: " << get_size_string(rss) << endl;
    osg::Matrix win;
    unsigned int validCount=0;
    for(int i=0; i < (int)cells.size(); i++) {
        if(cells[i].name != "null")
            validCount++;
    }
    osg::Timer_t startTick = osg::Timer::instance()->tick();
    formatBar("Img",startTick,0,validCount);
    FILE *logfp=fopen("DItiming.txt","w");

    int count=0;
    for(int i=0; i < (int)cells.size(); i++)
    {
        osg::Timer_t loopTick = osg::Timer::instance()->tick();
        if(cells[i].name == "null" )
            continue;

        if(nogfx) {
            char tmp[1024];
            {
                sprintf(tmp,"mosaic/image_r%04d_c%04d_rs%04d_cs%04d.%s",cells[i].row,cells[i].col,_tileRows,_tileColumns,ext.c_str());
                if(osgDB::fileExists(tmp)) {
                    vips::VImage tmpI(tmp);
                    raw.insertplace(tmpI.extract_bands(0,3),width*cells[i].col,height*(_tileRows-cells[i].row-1));
                } else {
                    vips::VImage tmpI;
                    tmpI.initdesc(width,height,3,vips::VImage::FMTUCHAR,vips::VImage::NOCODING,vips::VImage::sRGB,1.0,1.0,0,0);
                    memset(tmpI.data(),255,width*height*3);
                    raw.insertplace(tmpI,width*cells[i].col,height*(_tileRows-cells[i].row-1));
                    fprintf(stderr,"Can't find %s\n",tmp);
                }
            }
            if(untex) {
                sprintf(tmp,"mosaic/image_r%04d_c%04d_rs%04d_cs%04d_untex.%s",cells[i].row,cells[i].col,_tileRows,_tileColumns,ext.c_str());
                if(osgDB::fileExists(tmp)) {
                    vips::VImage tmpI(tmp);
                    raw_untex.insertplace(tmpI.extract_bands(0,3),width*cells[i].col,height*(_tileRows-cells[i].row-1));
                } else {
                    vips::VImage tmpI;
                    tmpI.initdesc(width,height,3,vips::VImage::FMTUCHAR,vips::VImage::NOCODING,vips::VImage::sRGB,1.0,1.0,0,0);
                    memset(tmpI.data(),255,width*height*3);
                    raw.insertplace(tmpI,width*cells[i].col,height*(_tileRows-cells[i].row-1));
                    fprintf(stderr,"Can't find %s\n",tmp);
                }
            }
        } else {


            osgViewer::Viewer viewer(arguments);



            GLenum readBuffer = GL_BACK;
            WindowCaptureCallback::FramePosition position = WindowCaptureCallback::END_FRAME;
            WindowCaptureCallback::Mode mode = WindowCaptureCallback::SINGLE_PBO;


            osg::ref_ptr<osg::GraphicsContext> pbuffer;
            {
                osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
                traits->x = 0;
                traits->y = 0;
                traits->width = width;
                traits->height = height;
                traits->red = 8;
                traits->green = 8;
                traits->blue = 8;
                traits->alpha = 8;
                traits->windowDecoration = false;
                traits->pbuffer = true;
                traits->doubleBuffer = true;
                traits->sharedContext = 0;

                pbuffer = osg::GraphicsContext::createGraphicsContext(traits.get());
                //std::cout << "Buffer obj "<< pbuffer->getState()->getMaxBufferObjectPoolSize() << " tex "<<  pbuffer->getState()->getMaxBufferObjectPoolSize() <<std::endl;
                if (pbuffer.valid())
                {
                    //   osg::notify(osg::INFO)<<"Pixel buffer has been created successfully."<<std::endl;
                }
                else
                {
                    osg::notify(osg::INFO)<<"Pixel buffer has not been created successfully."<<std::endl;
                }

            }



            osg::ref_ptr<WindowCaptureCallback> wcc=new WindowCaptureCallback(mode, position, readBuffer);
            osg::ref_ptr<osg::Camera> camera;

            if (pbuffer.valid())
            {   camera = new osg::Camera;
                camera->setGraphicsContext(pbuffer.get());
                camera->setViewport(new osg::Viewport(0,0,width,height));
                GLenum buffer = pbuffer->getTraits()->doubleBuffer ? GL_BACK : GL_FRONT;
                camera->setDrawBuffer(buffer);
                camera->setReadBuffer(buffer);
                camera->setFinalDrawCallback(wcc);

                if (pbufferOnly)
                {
                    viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd());

                    viewer.realize();
                }
                else
                {
                    viewer.realize();

                    viewer.stopThreading();

                    pbuffer->realize();

                    viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd());

                    viewer.startThreading();
                }
            }
            else
            {
                viewer.realize();

                addCallbackToViewer(viewer, wcc);
            }
            osg::Timer_t timeEndSetup = osg::Timer::instance()->tick();
            //double setupTime = osg::Timer::instance()->delta_s(loopTick, timeEndSetup);

            // load the data
            osg::Matrixd offsetMatrix=osg::Matrixd::scale(_tileColumns, _tileRows, 1.0)*osg::Matrixd::translate(_tileColumns-1-2*cells[i].col, _tileRows-1-2*cells[i].row, 0.0);
            /* printf("\r%03d/%03d",i,(int)cells.size());
            fflush(stdout);
            */       // std::cout <<"row: "<<cells[i].row << " col: "<<cells[i].col<<" tc: "<<_tileColumns << " "<<_tileRows<<" "<<"\n"<<osg::Matrix::scale(_tileColumns, _tileRows, 1.0) << "\n"<<osg::Matrix::translate(_tileColumns-1-2*cells[i].col, _tileRows-1-2*cells[i].row, 0.0) <<"\n"<<offsetMatrix<<endl;

            osg::ref_ptr<osg::Node> node=osgDB::readNodeFile(cells[i].name);
            osg::Timer_t timeEndLoad = osg::Timer::instance()->tick();

            double loadTime = osg::Timer::instance()->delta_s(timeEndSetup, timeEndLoad);

            int mem=0;
            if (node.valid() )
            {
                viewer.setSceneData( node );
                viewer.getCamera()->setProjectionMatrix(proj*offsetMatrix);
                viewer.getCamera()->setViewMatrix(view);
                viewer.frame();
                viewer.advance();
                viewer.updateTraversal();
                viewer.renderingTraversals();
                osg::Timer_t timeEndRender = osg::Timer::instance()->tick();
                double renderTime = osg::Timer::instance()->delta_s(timeEndLoad, timeEndRender);

                gpuUsage(1,mem);
                fprintf(logfp,"cnt: %d load %.1fs render: %.1fs mem1: %d MB",count,loadTime,renderTime,mem);
                osg::Image *img=(wcc->getContextData(pbuffer)->_imageBuffer[wcc->getContextData(pbuffer)->_currentImageIndex]);
                vips::VImage tmp(img->data(),img->s(),img->t(),4,vips::VImage::FMTUCHAR);
                raw.insertplace(tmp.flipver().extract_bands(0,3),width*cells[i].col,height*(_tileRows-cells[i].row-1));
                if(untex) {
                    node->getOrCreateStateSet()->addUniform(new osg::Uniform("shaderOut",3));
                    viewer.setSceneData( node );
                    viewer.frame();
                    viewer.advance();
                    viewer.updateTraversal();
                    viewer.renderingTraversals();
                    osg::Timer_t timeEndRender2 = osg::Timer::instance()->tick();
                    double renderTime2 = osg::Timer::instance()->delta_s(timeEndRender, timeEndRender2);

                    gpuUsage(1,mem);
                    osg::Image *img=(wcc->getContextData(pbuffer)->_imageBuffer[wcc->getContextData(pbuffer)->_currentImageIndex]);
                    vips::VImage tmp(img->data(),img->s(),img->t(),4,vips::VImage::FMTUCHAR);
                    raw_untex.insertplace(tmp.flipver().extract_bands(0,3),width*cells[i].col,height*(_tileRows-cells[i].row-1));
                    fprintf(logfp," render2: %.1fs mem2: %d",renderTime2,mem);

                }
            } else {
                std::cout << "Invalid " << cells[i].name << "\n";
            }
        }
        formatBar("Img",startTick,++count,validCount);

        osg::Timer_t timeEndLoop = osg::Timer::instance()->tick();
        double loopTime = osg::Timer::instance()->delta_s(loopTick, timeEndLoop);
        fprintf(logfp," loop: %.1fs\n",loopTime);
        fflush(logfp);


    }
    formatBar("Img",startTick,validCount,validCount);
    osg::Timer_t writeStart = osg::Timer::instance()->tick();
    process_mem_usage(vm, rss);
    cout << "VM: " << get_size_string(vm) << "; RSS: " << get_size_string(rss) << endl;
    raw.write("subtile.v");
    double writeTime = osg::Timer::instance()->delta_s(writeStart, osg::Timer::instance()->tick());
    fprintf(logfp,"Write Time %.1fs\n",writeTime);

    if(untex) {
        osg::Timer_t writeStart = osg::Timer::instance()->tick();

        raw_untex.write("subtile_untex.v");
        double writeTime = osg::Timer::instance()->delta_s(writeStart, osg::Timer::instance()->tick());
        fprintf(logfp,"Write Time 2 %.1fs\n",writeTime);

    }
    printf("Done\n");
    fclose(logfp);

    applyGeoTags("subtile.tif",osg::Vec2(lat,lon),view*proj,raw.Xsize(),raw.Ysize(),basepath);

}