int TwoKeyedAddress::readFromData(const Tbyte* data) {
  // Count of bytes read
  int count = 0;

  // Deallocate existing memory
  deallocateMemory();
  
  // Get number of primary keys
  numPrimaryKeys_ = ByteConversion::fromBytes(
                          data + count,
                          ByteSizes::uint16Size,
                          EndiannessTypes::little,
                          SignednessTypes::nosign);
  count += ByteSizes::uint16Size;
  
  // Allocate tables
  data_ = new Taddress*[numPrimaryKeys_];
  subKeyTable_ = new int[numPrimaryKeys_];
  
  // Write each key and data
  for (int i = 0; i < numPrimaryKeys_; i++) {
    // Get number of subkeys
    int numSubKeys = ByteConversion::fromBytes(
                            data + count,
                            ByteSizes::uint16Size,
                            EndiannessTypes::little,
                            SignednessTypes::nosign);
    count += ByteSizes::uint16Size;
    
    // Place in subkey table
    subKeyTable_[i] = numSubKeys;
    
    // Allocate memory for associated values, if they exist
    if (numSubKeys != 0) {
      data_[i] = new Taddress[numSubKeys + subKeyBase];
    }
    else {
      data_[i] = NULL;
    }
    
    // Read each subkey
    for (int j = 0; j < subKeyTable_[i]; j++) {
      int primarykey = i + primaryKeyBase;
      int subkey = j + subKeyBase;
      
      // Read address (stored as 32-bit)
      refDataByKeys(primarykey, subkey) = 
          ByteConversion::fromBytes(data + count,
                                    ByteSizes::uint32Size,
                                    EndiannessTypes::little,
                                    SignednessTypes::nosign);
      count += ByteSizes::uint32Size;
    }
    
  }
  
  // Return number of bytes read
  return count;
}
Exemple #2
0
bool
OfxMemory::freeMem()
{
    // A plug-in is calling freeMem, either this memory is held on the effect itself, in which case
    // calling releasePluginMemory will decrease the reference count and delete it.
    // If not held by an effect delete the memory because it's not held by a plug-in.
    deallocateMemory();
    EffectInstancePtr effect = _effect.lock();
    if (effect) {
        effect->releasePluginMemory(this);
        return false; // the call to releasePluginMemory is in charge of deleting this
    } else {
        return true; // object can be deleted
    }
}
Exemple #3
0
void *reallocateMemory(void *old, unsigned size_in_bytes)
{
    void *ptr = allocateMemory(size_in_bytes);

    if (old)
    {
        int *old_ptr = (int *)old;
        int size = std::min(old_ptr[-1] * -1, ((int)size_in_bytes + 3) / 4);

        for (int i = 0; i < size; i++)
        {
            ((int *)ptr)[i] = old_ptr[i];
        }
    }

    deallocateMemory(old);
    return ptr;
}
Exemple #4
0
bool
OfxMemory::alloc(size_t nBytes)
{

    QMutexLocker l(&_lock);

    if (_lockedCount) {
        return false;
    }
    deallocateMemory();

    PluginMemAllocateMemoryArgs args(nBytes);
    try {
        allocateMemory(args);
    } catch (const std::bad_alloc &) {
        return false;
    }

    return true;
}
/*---< cluster() >-----------------------------------------------------------*/
int cluster(int      npoints,				/* number of data points */
		int      nfeatures,				/* number of attributes for each point */
		float  **features,			/* array: [npoints][nfeatures] */                  
		int      min_nclusters,			/* range of min to max number of clusters */
		int		 max_nclusters,
		float    threshold,				/* loop terminating factor */
		int     *best_nclusters,		/* out: number between min and max with lowest RMSE */
		float ***cluster_centres,		/* out: [best_nclusters][nfeatures] */
		float	*min_rmse,				/* out: minimum RMSE */
		int		 isRMSE,				/* calculate RMSE */
		int		 nloops					/* number of iteration for each number of clusters */
	   )
{    
	int		nclusters;						/* number of clusters k */	
	int		index =0;						/* number of iteration to reach the best RMSE */
	int		rmse;							/* RMSE for each clustering */
	int    *membership;						/* which cluster a data point belongs to */
	float **tmp_cluster_centres;			/* hold coordinates of cluster centers */
	int		i;

	/* allocate memory for membership */
	membership = (int*) malloc(npoints * sizeof(int));

	/* sweep k from min to max_nclusters to find the best number of clusters */
	for(nclusters = min_nclusters; nclusters <= max_nclusters; nclusters++)
	{
		if (nclusters > npoints) break;	/* cannot have more clusters than points */

		/* allocate device memory, invert data array (@ kmeans_cuda.cu) */
		allocateMemory(npoints, nfeatures, nclusters, features);

		/* iterate nloops times for each number of clusters */
		for(i = 0; i < nloops; i++)
		{
			/* initialize initial cluster centers, CUDA calls (@ kmeans_cuda.cu) */
			tmp_cluster_centres = kmeans_clustering(features,
					nfeatures,
					npoints,
					nclusters,
					threshold,
					membership);

			if (*cluster_centres) {
				free((*cluster_centres)[0]);
				free(*cluster_centres);
			}
			*cluster_centres = tmp_cluster_centres;


			/* find the number of clusters with the best RMSE */
			if(isRMSE)
			{
				rmse = rms_err(features,
						nfeatures,
						npoints,
						tmp_cluster_centres,
						nclusters);

				if(rmse < min_rmse_ref){
					min_rmse_ref = rmse;			//update reference min RMSE
					*min_rmse = min_rmse_ref;		//update return min RMSE
					*best_nclusters = nclusters;	//update optimum number of clusters
					index = i;						//update number of iteration to reach best RMSE
				}
			}			
		}

	}
	deallocateMemory();							/* free device memory (@ kmeans_cuda.cu) */
	free(membership);

	return index;
}
int main (int argc, char* argv[])
{
	int id;  //  process rank
	int p;   //  number of processes
	//int num;
	int nMax;

	MPI_Init (&argc, &argv);
	MPI_Comm_rank (MPI_COMM_WORLD, &id);
	MPI_Comm_size (MPI_COMM_WORLD, &p);

	//FILE *fin;
	controlStruct control;
	acfStruct acfStructure;
	noiseStruct noiseStructure;

	char fname[1024];   // read in parameter file
	char Tname[1024];   // read in scintillation timescale

	int nt;
	double *tdiss;

	//char oname[1024];   // output file name
	//char pname[1024];   // file to plot
	char dname[1024];   // graphics device

	int i;
	int n = 1;  // number of dynamic spectrum to simulate

	double flux0;
	double flux1;

	int plotMode = 0;  // plot existing dynamic spectrum, off by default
	control.noplot = 1;  // don't show dynamic spectrum while simulationg, on by default

	// read options
	for (i=0;i<argc;i++)
	{
		if (strcmp(argv[i],"-f") == 0)
		{
			strcpy(fname,argv[++i]);
			strcpy(Tname,argv[++i]);
			printf ("Parameters are in %s\n", fname);
		}
		//else if (strcmp(argv[i],"-o")==0)
		//{
		//	strcpy(oname,argv[++i]);
		//	//printf ("Dynamic spectrum is output into %s\n", oname);
		//}
		else if (strcmp(argv[i],"-n")==0)
		{
			n = atoi (argv[++i]);
			printf ("Number of dynamic spectrum to simulate %d\n", n);
		}
		//else if (strcmp(argv[i],"-p")==0) // just plot 
		//{
		//	strcpy(pname,argv[++i]);
		//	plotMode = 1;
		//	printf ("Plotting exiting dynamic spectrum.\n");
		//}
		//else if (strcmp(argv[i],"-noplot")==0)
		//{
		//	control.noplot = 1; // Don't show dynamic spetrum while simulationg
		//}
		//else if (strcmp(argv[i],"-dev")==0)
		//{
		//	strcpy(dname,argv[++i]);
		//	printf ("Graphic device: %s\n", dname);
		//}
	}

	nt = readDissNum (Tname);
	tdiss = (double *)malloc(sizeof(double)*nt);
	readDiss (Tname, tdiss);

	sprintf(dname, "%s", "1/xs");
	/*
	if ((fin=fopen(oname, "w"))==NULL)
	{
		printf ("Can't open file...\n");
		exit(1);
	}
	*/

	if (plotMode==0)
	{
		// Simulate dynamic spectrum
		// read parameters
		initialiseControl(&control);
		readParams (fname,dname,n,&control);
		//readParams (fname,oname,dname,n,&control);
		printf ("Finished reading parameters.\n");

		//preAllocateMemory (&acfStructure, &control);
		//allocateMemory (&acfStructure);


		//num = (int)((control.scint_ts1-control.scint_ts0)/control.scint_ts_step);
		//for (tdiff=control.scint_ts0; tdiff<control.scint_ts1; tdiff+=control.scint_ts_step)
		//for (i=id; i<=num; i+=p)
		for (i=id; i<nt; i+=p)
		{
			control.nsub = tdiss[i];
			control.tsub = control.T/(double)(control.nsub);
			control.scint_ts = control.tsub/2.0;

			control.whiteLevel = control.whiteLevel0*sqrt(control.nsub*control.nchan);  // 0.1 gives 1 to a 10*10 dynamic spectrum
				
			calNoise (&noiseStructure, &control);
			//printf ("%lf\n", noiseStructure.detection);
			//printf ("tdiff fdiff: %lf %lf\n", tdiff, fdiff);
			flux0 = control.cFlux0;
			flux1 = control.cFlux1;

			acfStructure.probability = 1.0;
				
			calculateScintScale (&acfStructure, &control);
			//printf ("calculateScintScal\n");

			nMax = 0;
			while (fabs(acfStructure.probability-0.8)>=PRECISION && nMax <= 100)
			{
				control.cFlux = flux0+(flux1-flux0)/2.0;
				//printf ("%lf %lf %.8lf %.3f\n", tdiff, fdiff, control.cFlux, acfStructure.probability);
		
				// simulate dynamic spectra
				calculateNDynSpec (&acfStructure, &control, &noiseStructure);

				//if (control.noplot==0 && n == 1)
				//{
				//	// plot while simulating
				//	heatMap (&acfStructure, dname);
				//}

				// merged into calculateNDynSpec
				//qualifyVar (&acfStructure, &noiseStructure, &control);

				if (acfStructure.probability>0.8)
				{
					flux1 = control.cFlux;
				}
				else 
				{
					flux0 = control.cFlux;
				}
				nMax++;
				//printf ("%lf %f %d\n", control.cFlux, acfStructure.probability, nMax);
			}
			
			printf ("%d %lf %lf %f %lf %d\n", control.nsub, control.whiteLevel, noiseStructure.detection, control.cFlux, acfStructure.probability, nMax);
		
			deallocateMemory (&acfStructure);
		}

		fflush (stdout);
		MPI_Finalize ();

		printf ("Threshold: %f \n", noiseStructure.detection);
		// deallocate memory
	}
	//else
	//{
	//	plotDynSpec(pname, dname);
	//}

	/*
	if (fclose(fin))
	{
		printf ("Can't close file...\n");
		exit(1);
	}
	*/

	free(tdiss);
	return 0;
}