Beispiel #1
0
void create_grid(GRID_INFO * grid){
  int old_rank;
  int size;
  MPI_Comm_rank(MPI_COMM_WORLD,&old_rank);
  
#ifdef DEBUG_MODE
  if(old_rank == 0) {   DEBUG("Creating grid ...") }
#endif
  
  /* Setting up order of grid */
  MPI_Comm_size(MPI_COMM_WORLD,&size);
  grid->processes = size;
  grid->grid_order = sqrt(size);
  
#ifdef DEBUG_MODE
  if(old_rank == 0){ DEBUGA(" Order %d",grid->grid_order)};
#endif
  
  int dimensions[2] = {grid->grid_order,grid->grid_order};
  int periods[2] = {1,1};

  /* Creating the grid */
  MPI_Cart_create(MPI_COMM_WORLD,2,dimensions,periods,1, &(grid->grid_comm));


  /* Get rank in the grid */
  MPI_Comm_rank(grid->grid_comm,&(grid->rank));

  /* Get coordinates in the grid */
  int coord[2];
  MPI_Cart_coords(grid->grid_comm,grid->rank, 2, coord);
  grid->row = coord[0];
  grid->col = coord[1];

#ifdef DEBUG_MODE
  if(old_rank == 0) {  DEBUG(" Creating row communicator") }
#endif


  /* Creating row communicator */
  int variable_coord[2] = {0,1};
  MPI_Cart_sub(grid->grid_comm,variable_coord,&(grid->row_comm));


#ifdef DEBUG_MODE
  if(old_rank == 0) {  DEBUG(" Creating col communicator") }
#endif
    
  /* Creating column communicator */
  variable_coord[0] = 1;
  variable_coord[1] = 0;
  MPI_Cart_sub(grid->grid_comm,variable_coord,&(grid->col_comm));
    

#ifdef DEBUG_MODE
  if(old_rank == 0) {  DEBUG("Grid created.") }
#endif
}
Beispiel #2
0
int main(int argc, char** argv){
  int parent_tid = pvm_parent();
  int my_tid = pvm_mytid();
  DEBUGA("[TID  %d] Slave starting ...",my_tid);
  /* Checking number of arguments */
  if(argc<=3){
    printf("Error in arguments");
    exit(1);
  }


  /* Getting password from arguments */
  int number_of_threads = atoi(argv[3]);
  int posi = atoi(argv[4]);
  int pass_size = atoi(argv[1]);

  char * password = malloc(sizeof(char)*(pass_size+1));
  password[0] = '\0';
  strncat(password,argv[2],pass_size);
  password[pass_size] = '\0';

  DEBUGA("[TID  %d] Password %s Size %d",my_tid,password,pass_size);


  char * temp_password = malloc(sizeof(char)*(pass_size+1));
  char * interval_start = malloc(sizeof(char)*(pass_size+1));
  char * interval_end = malloc(sizeof(char)*(pass_size+1));
  char * recv_buffer = malloc(sizeof(char)*MAX_SIZE);
  recv_buffer[0] = '\0';
  unsigned long long interval_size;

  int buf_size,mes_tag,mes_tid,buf_id;

    
  /* Ask parent for new interval */
  pvm_initsend(PvmDataDefault);
  pvm_pkint(&my_tid,1,1);
  pvm_send(parent_tid,NEED_INTERVAL);
  buf_id = pvm_recv(parent_tid,-1);
  pvm_bufinfo(buf_id,&buf_size,&mes_tag,&mes_tid);

  if(mes_tag == NEW_INTERVAL){
    pvm_upkstr(recv_buffer);
  }
  else if(mes_tag == NO_NEW_INTERVAL){
    exit(1);
  }
  else{
    exit(1);
  }
 
  /* Extract data from buffer */
  interval_start[0] = '\0';
  interval_end[0] = '\0';
  temp_password[0]='\0';
  strncpy(interval_start,recv_buffer,pass_size);
  interval_start[pass_size] = '\0';
  strncpy(interval_end,recv_buffer+pass_size,pass_size);
  interval_end[pass_size] = '\0';
  
  pthread_mutex_lock(&mutex_i_manager);
  init_interval_manager(&i_manager,
			interval_start,
			interval_end,
			password,
			posi);
  pthread_mutex_unlock(&mutex_i_manager);


  /* Creating threads ... */
  pthread_t * threads = malloc(sizeof(pthread_t)*number_of_threads);
  pthread_attr_t attr;
  int c_thread = 0;
  for(c_thread = 0; c_thread < number_of_threads ; ++c_thread){
    pthread_create(&threads[c_thread],&attr,find_password_thread,(void*)NULL);
  }

    
  while(1){
    

    pthread_mutex_lock(&mutex_action);
    pthread_mutex_unlock(&mutex_slave_waiting);
    pthread_cond_wait(&cond_action,&mutex_action);
    pthread_mutex_lock(&mutex_slave_waiting);

    if(action == NEW_INTERVAL){
      pvm_initsend(PvmDataDefault);
      pvm_pkint(&my_tid,1,1);
      pvm_send(parent_tid,NEED_INTERVAL);
      buf_id = pvm_recv(parent_tid,-1);
      pvm_bufinfo(buf_id,&buf_size,&mes_tag,&mes_tid);
      
      if(mes_tag == NEW_INTERVAL){
	pvm_upkstr(recv_buffer);
	interval_start[0] = '\0';
	interval_end[0] = '\0';
	strncpy(interval_start,recv_buffer,pass_size);
	interval_start[pass_size] = '\0';
	strncpy(interval_end,recv_buffer+pass_size,pass_size);
	interval_end[pass_size] = '\0';
	DEBUGA("New interval start interval : %s end interval : %s",interval_start,interval_end);
	pthread_mutex_lock(&mutex_i_manager);
	set_new_interval(&i_manager,interval_start,interval_end);
	pthread_mutex_unlock(&mutex_i_manager);

      }
      else if(mes_tag == NO_NEW_INTERVAL){
	
      }
      else{
      }
    }
    else if (action == FOUND_PASSWORD){
      pthread_mutex_lock(&mutex_f_password);
      pthread_mutex_lock(&mutex_f_c_password);
      
      DEBUGA("[TID  %d] Found Password",my_tid);
      pvm_initsend( PvmDataDefault );
      pvm_pkstr(found_char_password);
      pvm_send(parent_tid,FOUND_PASSWORD);
      pthread_mutex_unlock(&mutex_f_password);
      pthread_mutex_unlock(&mutex_f_c_password);
      break;

    }
    pthread_mutex_unlock(&mutex_action);
    

  }


 
  
  pvm_send(parent_tid,CHILD_TERMINATE);
  DEBUGA("[TID  %d] Terminating slave",my_tid);

  free(password);
  free(temp_password);
  free(interval_start);
  free(interval_end);
  free(recv_buffer);

  return EXIT_SUCCESS;
}
Beispiel #3
0
/* Matlab Gateway routine */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
	char	*pszSRS_WKT = NULL;
	GDALDatasetH	hSrcDS, hDstDS;
	GDALDriverH	hDriver;
	GDALRasterBandH hBand;
	OGRSpatialReference oSrcSRS, oDstSRS; 
	GDALResampleAlg	interpMethod = GRA_NearestNeighbour;
	GDALTransformerFunc pfnTransformer = NULL;
	CPLErr		eErr;
	GDAL_GCP	*pasGCPs = NULL;
	void		*hTransformArg;
	static int runed_once = FALSE;	/* It will be set to true if reaches end of main */

	int	nx, ny, i, j, m, n, c = 0, n_pts, n_fields;
	char	*pszSrcSRS = NULL, *pszSrcWKT = NULL;
	char	*pszDstSRS = NULL, *pszDstWKT = NULL;
	double	*in_data, *ptr_d, *x, *y, *z;
	mxArray	*mx_ptr;

	int	nGCPCount = 0, nOrder = 0;
	char	**papszMetadataOptions = NULL;
	char	*tmp, *txt;
	int	bInverse = FALSE;
	int	*bSuccess;
	char	**papszTO = NULL;


	if (nrhs == 2 && mxIsStruct(prhs[1])) {

		/* -------------------------------------------------- */

		/* -------- See for projection stuff ---------------- */
		mx_ptr = mxGetField(prhs[1], 0, "SrcProjSRS");
		if (mx_ptr != NULL)
			pszSrcSRS = (char *)mxArrayToString(mx_ptr);

		mx_ptr = mxGetField(prhs[1], 0, "SrcProjWKT");
		if (mx_ptr != NULL)
			pszSrcWKT = (char *)mxArrayToString(mx_ptr);

		mx_ptr = mxGetField(prhs[1], 0, "DstProjSRS");
		if (mx_ptr != NULL)
			pszDstSRS = (char *)mxArrayToString(mx_ptr);

		mx_ptr = mxGetField(prhs[1], 0, "DstProjWKT");
		if (mx_ptr != NULL)
			pszDstWKT = (char *)mxArrayToString(mx_ptr);
		/* -------------------------------------------------- */

		/* -------- Do we have GCPs? ----------------------- */
		mx_ptr = mxGetField(prhs[1], 0, "gcp");
		if (mx_ptr != NULL) {
			nGCPCount = mxGetM(mx_ptr);
			if (mxGetN(mx_ptr) != 4)
				mexErrMsgTxt("GDALWARP: GCPs must be a Mx4 array");
			ptr_d = mxGetPr(mx_ptr);
			pasGCPs = (GDAL_GCP *) mxCalloc( nGCPCount, sizeof(GDAL_GCP) );
			GDALInitGCPs( 1, pasGCPs + nGCPCount - 1 );
			for (i = 0; i < nGCPCount; i++) {
				pasGCPs[i].dfGCPPixel = ptr_d[i];
				pasGCPs[i].dfGCPLine = ptr_d[i+nGCPCount];
				pasGCPs[i].dfGCPX = ptr_d[i+2*nGCPCount];
				pasGCPs[i].dfGCPY = ptr_d[i+3*nGCPCount];
				pasGCPs[i].dfGCPZ = 0;
			}
		}
			/* ---- Have we an order request? --- */
		mx_ptr = mxGetField(prhs[1], 0, "order");
		if (mx_ptr != NULL) {
			ptr_d = mxGetPr(mx_ptr);
			nOrder = (int)*ptr_d;
			if (nOrder != -1 || nOrder != 0 || nOrder != 1 ||
				nOrder != 2 || nOrder != 3)
				nOrder = 0;
		}
		/* -------------------------------------------------- */
		mx_ptr = mxGetField(prhs[1], 0, "inverse");
		if (mx_ptr != NULL)
			bInverse = TRUE;

		mx_ptr = mxGetField(prhs[1], 0, "ResampleAlg");
		if (mx_ptr != NULL) {
			txt = (char *)mxArrayToString(mx_ptr);
			if (!strcmp(txt,"nearest"))
				interpMethod = GRA_NearestNeighbour;
			else if (!strcmp(txt,"bilinear"))
				interpMethod = GRA_Bilinear;
			else if (!strcmp(txt,"cubic") || !strcmp(txt,"bicubic"))
				interpMethod = GRA_Cubic;
			else if (!strcmp(txt,"spline"))
				interpMethod = GRA_CubicSpline;
		}

	}
	else {
		mexPrintf("Usage: out = gdaltransform_mex(IN,PAR_STRUCT)\n\n");
		mexPrintf("      IN is a Mx2 or Mx3 array of doubles with X, Y (,Z)\n");
		mexPrintf("      PAR_STRUCT is a structure with at most two of the next fields:\n");
		mexPrintf("\t\t'SrcProjSRS', 'SrcProjWKT' -> Source projection string\n");
		mexPrintf("\t\t'DstProjSRS', 'DstProjWKT' -> Target projection string\n");
		mexPrintf("\t\t\tSRS stands for a string of the type used by proj4\n");
		mexPrintf("\t\t\tWKT stands for a string on the 'Well Known Text' format\n\n");
		mexPrintf("\t\t\tIf one of the Src or Dst fields is absent a GEOGRAPHIC WGS84 is assumed\n");
		mexPrintf("\nOPTIONS\n");
		mexPrintf("\t\t'gcp' a [Mx4] array with Ground Control Points\n");
		mexPrintf("\t\t'inverse' reverse transformation. e.g from georef to rows-columns\n");

		if (!runed_once)		/* Do next call only at first time this MEX is loaded */
			GDALAllRegister();

        	mexPrintf( "The following format drivers are configured and support Create() method:\n" );
        	for( i = 0; i < GDALGetDriverCount(); i++ ) {
			hDriver = GDALGetDriver(i);
			if( GDALGetMetadataItem( hDriver, GDAL_DCAP_CREATE, NULL ) != NULL)
				mexPrintf("%s: %s\n", GDALGetDriverShortName(hDriver), 
							GDALGetDriverLongName(hDriver));
		}
		return;
	}


	if (!runed_once) 		/* Do next call only at first time this MEX is loaded */
		GDALAllRegister();


	/* ----- Check that first argument contains at least a mx2 table */
	n_pts = mxGetM (prhs[0]);
	n_fields = mxGetN(prhs[0]);
	if (!mxIsNumeric(prhs[0]) || (n_fields < 2)) {
		mexPrintf("GDALTRANSFORM ERROR: first argument must contain a mx2 (or mx3) table\n");
		mexErrMsgTxt("               with the x,y (,z) positions to convert.\n");
	}

DEBUGA(1);
	/* ---------- Set the Source projection ---------------------------- */
	/* If it was not provided assume it is Geog WGS84 */
	if (pszSrcSRS == NULL && pszSrcWKT == NULL)
		oSrcSRS.SetWellKnownGeogCS( "WGS84" ); 
	else if (pszSrcWKT != NULL)
		oSrcSRS.importFromWkt( &pszSrcWKT );

	else {
		if( oSrcSRS.SetFromUserInput( pszSrcSRS ) != OGRERR_NONE )
			mexErrMsgTxt("GDALTRANSFORM: Translating source SRS failed.");
	}
	if (pszSrcWKT == NULL)
		oSrcSRS.exportToWkt( &pszSrcWKT );

	/* ------------------------------------------------------------------ */


DEBUGA(2);
	/* ---------- Set up the Target coordinate system ------------------- */
	/* If it was not provided assume it is Geog WGS84 */
	CPLErrorReset();
	if (pszDstSRS == NULL && pszDstWKT == NULL)
		oDstSRS.SetWellKnownGeogCS( "WGS84" ); 
	else if (pszDstWKT != NULL)
		oDstSRS.importFromWkt( &pszDstWKT );
	else {
		if( oDstSRS.SetFromUserInput( pszDstSRS ) != OGRERR_NONE )
			mexErrMsgTxt("GDALTRANSFORM: Translating target SRS failed.");
	}
	if (pszDstWKT == NULL)
		oDstSRS.exportToWkt( &pszDstWKT );
	/* ------------------------------------------------------------------ */



	/* -------------------------------------------------------------------- */
	/*      Create a transformation object from the source to               */
	/*      destination coordinate system.                                  */
	/* -------------------------------------------------------------------- */
	if( nGCPCount != 0 && nOrder == -1 ) {
		pfnTransformer = GDALTPSTransform;
		hTransformArg = GDALCreateTPSTransformer( nGCPCount, pasGCPs, FALSE );
	}
	else if( nGCPCount != 0 ) {
DEBUGA(3);
		pfnTransformer = GDALGCPTransform;
		hTransformArg = GDALCreateGCPTransformer( nGCPCount, pasGCPs, nOrder, FALSE );
	}
	else {
		pfnTransformer = GDALGenImgProjTransform;
		//hTransformArg = GDALCreateGenImgProjTransformer2( hSrcDS, hDstDS, papszTO );
		hTransformArg = 
    			GDALCreateGenImgProjTransformer( NULL, pszSrcWKT, NULL, pszDstWKT, 
						 nGCPCount == 0 ? FALSE : TRUE, 0, nOrder );
	}

	//CSLDestroy( papszTO );
DEBUGA(4);

	if( hTransformArg == NULL )
		mexErrMsgTxt("GDALTRANSFORM: Generating transformer failed.");

	in_data = (double *)mxGetData(prhs[0]);

	if (n_pts == 1) {
		x = &in_data[0];
		y = &in_data[1];
		bSuccess = &c;
	}
	else {
		x = (double *)mxCalloc(n_pts, sizeof(double));
		y = (double *)mxCalloc(n_pts, sizeof(double));
		bSuccess = (int *)mxCalloc(n_pts, sizeof(int));

		for (j = 0; j < n_pts; j++) {
			x[j] = in_data[j];
			y[j] = in_data[j+n_pts];
		}
	}
DEBUGA(5);

	z = (double *)mxCalloc(n_pts, sizeof(double));
	if (n_fields == 3)
		for (j = 0; j < n_pts; j++)
			z[j] = in_data[j+2*n_pts];

	if ( !pfnTransformer( hTransformArg, bInverse, n_pts, x, y, z, bSuccess ) )
		mexPrintf( "Transformation failed.\n" );


DEBUGA(6);
	if( nGCPCount != 0 && nOrder == -1 )
		GDALDestroyTPSTransformer(hTransformArg);
	else if( nGCPCount != 0 )
		GDALDestroyGCPTransformer(hTransformArg);
	else
		GDALDestroyGenImgProjTransformer(hTransformArg);


	/* -------------- Copy the result into plhs  --------------------------------- */
	plhs[0] = mxCreateDoubleMatrix (n_pts,n_fields, mxREAL);
	ptr_d = mxGetPr(plhs[0]);

	for (j = 0; j < n_pts; j++) {
		ptr_d[j] = x[j];
		ptr_d[j+n_pts] = y[j];
	}

	if (n_pts > 1) {
		mxFree((void *)x);	mxFree((void *)y);	mxFree((void *)bSuccess);
	}

	if (n_fields == 3) {
		for (j = 0; j < n_pts; j++)
			ptr_d[j+2*n_pts] = z[j];
	}
	mxFree((void *)z);


	if (pszDstWKT && strlen(pszDstWKT) > 1 ) OGRFree(pszDstWKT);	
	if (pszSrcWKT && strlen(pszSrcWKT) > 1 ) OGRFree(pszSrcWKT);
	//OGRFree(pszSrcWKT);
	//OGRFree(pszDstWKT);
	if (nGCPCount > 0) {
		GDALDeinitGCPs( nGCPCount, pasGCPs );	// makes this mex crash in the next call
		mxFree((void *) pasGCPs );
	}

	runed_once = TRUE;	/* Signals that next call won't need to call GDALAllRegister() again */

}