Example #1
0
void Particles::InitSolid()
{
	//init solidPos	
	solidPos = new float[4];
	solidPos[0] = 20.0f;
	solidPos[1] = 10.0f;
	solidPos[2] = 150.0f;
	solidPos[3] = 0.0f;
	cudaMalloc((void **)&solidPosGpu, 4 * sizeof(float));
	copyArrayToDevice(solidPosGpu, solidPos, 0, 4 * sizeof(float));

	//init solid vel
	solidVel = new float[4];
	memset(solidVel, 0, sizeof(float)* 4);
	allocateArray((void **)&solidVelGpu, sizeof(float)* 4);
	cudaMemset(solidVelGpu, 0, sizeof(float)* 4);

	//buoyancy
	buoyancy = new float[4 * numParticles];
	memset(buoyancy, 0, sizeof(float)* 4 * numParticles);
	allocateArray((void **)&buoyancyGpu, sizeof(float)* 4 * numParticles);
	cudaMemset(buoyancyGpu, 0, sizeof(float)* 4 * numParticles);

	//buoyancy angular velocity
	buoyancyAng = new float[4 * numParticles];
	memset(buoyancyAng, 0, sizeof(float)* 4 * numParticles);
	allocateArray((void **)&buoyancyAngGpu, sizeof(float)* 4 * numParticles);
	cudaMemset(buoyancyAngGpu, 0, sizeof(float)* 4 * numParticles);
	
}
void ParticleSystem::_initialize(int numParticles){
    assert(!m_bInitialized);
    m_numParticles = numParticles;

    //Allocate host storage
    m_hPos          = (float *)malloc(m_numParticles * 4 * sizeof(float));
    m_hVel          = (float *)malloc(m_numParticles * 4 * sizeof(float));
    m_hReorderedPos = (float *)malloc(m_numParticles * 4 * sizeof(float));
    m_hReorderedVel = (float *)malloc(m_numParticles * 4 * sizeof(float));
    m_hHash         = (uint  *)malloc(m_numParticles * sizeof(uint));
    m_hIndex        = (uint  *)malloc(m_numParticles * sizeof(uint));
    m_hCellStart    = (uint  *)malloc(m_numGridCells * sizeof(uint));
    m_hCellEnd      = (uint  *)malloc(m_numGridCells * sizeof(uint));

    memset(m_hPos, 0, m_numParticles * 4 * sizeof(float));
    memset(m_hVel, 0, m_numParticles * 4 * sizeof(float));
    memset(m_hCellStart, 0, m_numGridCells * sizeof(uint));
    memset(m_hCellEnd,   0, m_numGridCells * sizeof(uint));

    //Allocate GPU data
    shrLog("Allocating GPU Data buffers...\n\n");
    allocateArray(&m_dPos,          m_numParticles * 4 * sizeof(float));
    allocateArray(&m_dVel,          m_numParticles * 4 * sizeof(float));
    allocateArray(&m_dReorderedPos, m_numParticles * 4 * sizeof(float));
    allocateArray(&m_dReorderedVel, m_numParticles * 4 * sizeof(float));
    allocateArray(&m_dHash,         m_numParticles * sizeof(uint));
    allocateArray(&m_dIndex,        m_numParticles * sizeof(uint));
    allocateArray(&m_dCellStart,    m_numGridCells * sizeof(uint));
    allocateArray(&m_dCellEnd,      m_numGridCells * sizeof(uint));

    if (!m_bQATest)
    {
        //Allocate VBO storage
        m_posVbo   = createVBO(m_numParticles * 4 * sizeof(float));
        m_colorVBO = createVBO(m_numParticles * 4 * sizeof(float));

        //Fill color buffer
        glBindBufferARB(GL_ARRAY_BUFFER, m_colorVBO);
        float *data = (float *)glMapBufferARB(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
            float *ptr = data;
            for(uint i = 0; i < m_numParticles; i++){
                float t = (float)i / (float) m_numParticles;
                #if 0
                    *ptr++ = rand() / (float) RAND_MAX;
                    *ptr++ = rand() / (float) RAND_MAX;
                    *ptr++ = rand() / (float) RAND_MAX;
                #else
                    colorRamp(t, ptr);
                    ptr += 3;
                #endif
                *ptr++ = 1.0f;
            }
        glUnmapBufferARB(GL_ARRAY_BUFFER);
    }

    setParameters(&m_params);
    setParametersHost(&m_params);
    m_bInitialized = true;
}
Example #3
0
int main(){
	allocateArray();
	fillArray();
	printArray();
	freeArray();
	return 0;
}
Example #4
0
BBArray *bbArrayConcat( const char *type,BBArray *x,BBArray *y ){

	BBArray *arr;
	char *data;
	int length=x->scales[0]+y->scales[0];
	
	if( length<=0 ) return &bbEmptyArray;

	arr=allocateArray( type,1,&length );
	
	data=(char*)BBARRAYDATA( arr,1 );
	
	memcpy( data,BBARRAYDATA( x,1 ),x->size );
	memcpy( data+x->size,BBARRAYDATA( y,1 ),y->size );
	
#ifdef BB_GC_RC
	if( type[0]==':' || type[0]=='$' || type[0]=='[' ){
		int i;
		BBObject **p=(BBObject**)data;
		for( i=0;i<length;++i ){
			BBObject *o=*p++;
			BBINCREFS( o );
		}
	}
#endif
	return arr;
}
Example #5
0
BBArray *bbArrayNew1D( const char *type,int length ){

	BBArray *arr=allocateArray( type,1,&length );
	
	initializeArray( arr );
	
	return arr;
}
Example #6
0
BBArray *bbArrayNewEx( const char *type,int dims,int *lens ){

	BBArray *arr=allocateArray( type,dims,lens );
	
	initializeArray( arr );
	
	return arr;
}
Example #7
0
void Particles::InitMemory()
{
	assert(!initFlag);

	pos = new float[numParticles * 4];
	vel = new float[numParticles * 4];
	//density = new float[numParticles];
	memset(pos, 0, numParticles * 4 * sizeof(float));
	memset(vel, 0, numParticles * 4 * sizeof(float));
	//memset(density, 0, numParticles * sizeof(float));

	cellStart = new uint[numParticles];
	cellEnd = new uint[numParticles];
	memset(cellStart, 0, numParticles*sizeof(uint));
	memset(cellEnd, 0, numParticles*sizeof(uint));

	uint memSize = sizeof(float) * 4 * numParticles;
	posVbo = CreateVBO(memSize,POSITION);
	registerGLBufferObject(posVbo, &m_cuda_posvbo_resource);

	allocateArray((void **)&velGpu, memSize);
	allocateArray((void **)&sortedPos, memSize);
	allocateArray((void **)&sortedVel, memSize);
	allocateArray((void **)&gridParticleHash, numParticles*sizeof(uint));
	allocateArray((void **)&gridParticleIndex, numParticles*sizeof(uint));
	allocateArray((void **)&cellStart, (mparams.wholeNumCells)*sizeof(uint));
	allocateArray((void **)&cellEnd, (mparams.wholeNumCells)*sizeof(uint));

	InitColor();
	InitSolid();
	setParameters(&mparams);
	initFlag = true;
}
TrackArray::TrackArray( int width, int height, int cellSize ):
    TrackDataContainer( width, height, cellSize )
{
    // zapewnia, iz zarowno szerokosc, jak i wysokosc, sa potegami dwojki
    if( !IsPowerOf2(width) || !IsPowerOf2(height) )
        throw TrackContainerException();

    mpDataMatrix = allocateArray( width, height, cellSize, mCellSizeLogarithm );
}
Example #9
0
int main(){
	int i;
	int *vector = NULL;
	allocateArray(&vector, 5, 45);
	for(i = 0; i < 5; i++){
		printf("%d\n", vector[i]);
	}
	free(vector);
	return 0;
}
Example #10
0
int main(){
	int i;
	int* vector = allocateArray(5, 45);
	for(i=0; i<5; i++){
		printf("%d\n", vector[i]);
	}
	// 使い終わったメモリは解放しましょう。
	free(vector);
	return 0;
}
void ArrayList::openGap(int index) {
   if (isAtCapacity()) {
      allocateArray();
   }

   for (int i = getCount(); i > index; i--) {
      array[i] = array[i - 1];
   }

   if (current >= index) {
      current++;
   }
}
Example #12
0
void Foam::MytwoWayMPI::allocateArray
(
    int**& array,
    int initVal,
    int width,
    const char* length
) const
{
    int len=0;
    if(strcmp(length,"nparticles")==0) len = particleCloud_.numberOfParticles();
    else if (strcmp(length,"nbodies")==0) len = nClumpTypes_;
    else FatalError<<"*** call allocateArray with length, nparticles or nbodies!\n" << abort(FatalError);
    allocateArray(array,initVal,width,len);
}
Example #13
0
BBArray *bbArrayNew( const char *type,int dims,... ){

#if BB_ARGP
	int *lens=(int*)bbArgp(8);
#else
	int *lens=&dims+1;
#endif

	BBArray *arr=allocateArray( type,dims,lens );
	
	initializeArray( arr );
	
	return arr;
}
void Foam::dataExchangeModel::allocateArray
(
    double**& array,
    double initVal,
    int width,
    const char* length
) const
{
    int len=0;
    if(strcmp(length,"nparticles")==0) len = particleCloud_.numberOfParticles();
    else if (strcmp(length,"nbodies")==0) len = nClumpTypes_;
    else FatalError<<"call allocateArray with length, nparticles or nbodies!\n" << abort(FatalError);
    allocateArray(array,initVal,width,len);
}
Example #15
0
int main (int argc, char *argv[])
{
   struct timespec tStart;
   struct timespec tEnd;
   pthread_t *aThreads;
   THREAD_PARAMS *aParams;
   int64_t * aArray = NULL;

   int64_t iret = 0;
   int64_t cThreads = 0;
   int64_t cTasks = 0;
   bool fBlock = false;
   bool fSimple = true; 
   bool fRandom = false;

   iret = parseCommandLine(argc, argv, &cThreads, &cTasks, &fBlock, &fSimple, &fRandom);

   if (0 == iret) {
      iret = allocateArray(fRandom, cTasks, &aArray);
   }

   if (0 == iret) {
      iret = clock_gettime(CLOCK_REALTIME, &tStart);
   }
   
   if (0 == iret) {
      iret = createThreads(cThreads, cTasks, fBlock, fSimple, aArray, &aThreads, &aParams);
   }

   if (0 == iret) {
      iret = joinThreads(cThreads, cTasks, aThreads, aParams);
      aThreads = NULL;
      aParams = NULL;
   }

   clock_gettime(CLOCK_REALTIME, &tEnd);

   if (0 == iret) {
      //test(fSimple, cTasks, aArray);
   }

   free(aArray);

   struct timespec tsDiff = diff(tStart, tEnd);
   //printf("Elapsed Time: %d.%09d Sec.\n", tsDiff.tv_sec, tsDiff.tv_nsec);
   printf("%d, %d, %s, %s, %s, %d.%09d\n", cThreads, cTasks, fBlock ? "block":"cyclic", fSimple ? "negation":"factorial", fRandom ? "random":"ramp",tsDiff.tv_sec, tsDiff.tv_nsec);
   return(iret);
}
Example #16
0
BBArray *bbArrayFromData( const char *type,int length,void *data ){

	int k;
	BBArray *arr;

	if( length<=0 ) return &bbEmptyArray;
	
	arr=allocateArray( type,1,&length );

	if( type[0]=='b' ){
		unsigned char *p=BBARRAYDATA( arr,1 );
		for( k=0;k<length;++k ) p[k]=((int*)data)[k];
	}else if( type[0]=='s' ){
		unsigned short *p=BBARRAYDATA( arr,1 );
		for( k=0;k<length;++k ) p[k]=((int*)data)[k];
	}else{
		memcpy( BBARRAYDATA( arr,1 ),data,arr->size );
	}
	return arr;
}
Example #17
0
int main(int argc, const char* argv[])
{
	if( argc != 2 ) //checks for the input file name
	{
		printf( "error; no input file name\n" );
		return 1;
	}

	FILE *filePointer;
	filePointer = fopen( argv[1], "r" );
	char **arrayPointer;


	//char maze[MAX_MAZE_SIZE][MAX_MAZE_SIZE] = { 0 };

	int numberOfTestCases = 0;
	fscanf( filePointer, "%d\n", &numberOfTestCases );

	for( int testCaseNumber = 0; testCaseNumber < numberOfTestCases; testCaseNumber++ )
	{
        int size = 0;
        int * xy;
        int x;
        int y;
		fscanf( filePointer, "%d\n", &size );
		
		printf( "ENTER\n" );
        allocateArray(size, &arrayPointer);
		initializeArray(size, arrayPointer, filePointer);
		xy = findEntrance(size, arrayPointer);
		x = xy[0];
		y = xy[1];
		solveMaze(size, arrayPointer, x ,y);
		deallocateArray(size, arrayPointer);
		printf( "EXIT\n***\n" );		
	}


	fclose( filePointer );
	return 0;
}
void TrackArray::changeCellSize( int cellSize ) {
    double ratio = mCellSize / cellSize;
    DataMatrix* data_matrix;

    if( ratio != 1.0 ) {
        data_matrix = allocateArray( mWidth, mHeight, cellSize, mCellSizeLogarithm );

        if( ratio > 1.0 ) {
            // przypadek zlozony: przepisujemy...

            /* TODO: poprawic segfaulta
            int r = static_cast<int>( ratio );

            for( int j = mHeight; j != 0; j -= mCellSize ) {
            	for( int i = mWidth; i != 0; i -= mCellSize ) {
            		// przepisujemy komorke (i,j) oryginalnej macierzy do
            		// komorek (i*r,j*r) .. (i*r + r-1,j*r + r-1)
            		int xo = i*r;
            		int yo = j*r;
            		for( int y = r; y != 0; --y )
            			for( int x = r; x != 0; --x )
            				data_matrix->set( xo+x, yo+y, mpDataMatrix->get(i,j) );
            	}
            }
            */

        } else if( ratio < 1.0 ) {
            // przypadek prosty: zmniejszamy rozdzieczosc; nic nie przepisujemy
            //TODO: to nie powinno byc takie proste
        }

        delete mpDataMatrix;
        mpDataMatrix = data_matrix;
        mCellSize = cellSize;

    }
}
ArrayList::ArrayList(int initCapacity, int addCapacity): capacity(initCapacity),
additionalCapacity(addCapacity), array(NULL) {
   current = getCount();
   allocateArray();
}
ArrayList::ArrayList(): capacity(DEFAULT_INITIAL_CAPACITY),
additionalCapacity(DEFAULT_ADDITIONAL_CAPACITY), array(NULL) {
   current = getCount();
   allocateArray();
}
Example #21
0
void CalculateOrdinaryScore(FlowDist *flowDist, AlleleIdentity &variant_identity,
                            vcf::Variant ** candidate_variant, ControlCallAndFilters &my_controls,
                            bool *isFiltered, int DEBUG) {
  vector<float>* reflikelihoods = flowDist->getReferenceLikelihoods();
  vector<float>* varlikelihoods = flowDist->getVariantLikelihoods();
  int totalReads = reflikelihoods->size();
  const int totalHypotheses = 2;
  float **scoreLikelihoods;
  float refLikelihoods;
  float varLikelihoods;
  float scores[totalHypotheses]  = {0};
  int counts[totalHypotheses] = {0};
  float minDiff = 2.0;
  float minLikelihoods = 3.0;
  allocateArray(&scoreLikelihoods, totalReads, totalHypotheses);

  for (int i = 0; i < totalReads; i++) {
    refLikelihoods = reflikelihoods->at(i);
    varLikelihoods = varlikelihoods->at(i);
    if (DEBUG)
      cout << "ref likelihood = " << refLikelihoods << endl;
    scoreLikelihoods[i][0] = refLikelihoods;
    scoreLikelihoods[i][1] = varLikelihoods;
  }

  if (variant_identity.status.isSNP || variant_identity.status.isMNV) {
    minDiff = 1.0;
    minLikelihoods = 0.5;
  }

  calc_score_hyp(totalReads, totalHypotheses, scoreLikelihoods, scores, counts, minDiff, minLikelihoods);
  float BayesianScore;

  BayesianScore = scores[1];
  //string *filterReason = new string();

  //cout << "Bayesian Score = " << BayesianScore << endl;
  float stdBias = flowDist->summary_stats.getStrandBias();
  float refBias = flowDist->summary_stats.getRefStrandBias();
  //float baseStdBias = flowDist->summary_stats.getBaseStrandBias();



  /* moving filter operation to DecisionTree
  *isFiltered = filterVariants(filterReason, variant_identity.status.isSNP, variant_identity.status.isMNV, variant_identity.status.isIndel, variant_identity.status.isHPIndel,
                               false, BayesianScore, my_controls, 0, 0, 0, stdBias, refBias, baseStdBias,
                               flowDist->summary_stats.getAltAlleleFreq(), variant_identity.refHpLen, abs(variant_identity.inDelLength));

  flowDist->summary_info.isFiltered= *isFiltered;
  */

  flowDist->summary_info.alleleScore = BayesianScore;

  //flowDist->summary_info.filterReason = *filterReason;

  //now if the allele is a SNP and a possible overcall/undercall FP SNP, evaluate the score for HP lengths on either side of SNP
  //we move filtering to final stage so calculate confidence of HP length for all overcall/undercall snps
  if (variant_identity.status.isSNP && variant_identity.status.isOverCallUnderCallSNP &&
      (variant_identity.underCallLength+1 > 11 || variant_identity.overCallLength-1 > 11) ) {
    flowDist->summary_info.isFiltered = true;
    flowDist->summary_info.filterReason = "Overcall/Undercall_HP_SNP";
    flowDist->summary_info.alleleScore  = 0.0;
  }
  if (!flowDist->summary_info.isFiltered && variant_identity.status.isSNP && variant_identity.status.isOverCallUnderCallSNP) {
    float overCallHPScore = 0.0f;
    float underCallHPScore = 0.0f;
    bool isUnderCallRef = false;
    bool isOverCallRef = false;
    float underCallFreq = 0.0;
    float overCallFreq = 0.0;
    float maxProb = 0;
    int * peak_finding_tuning_parameters = new int[9];
    peak_finding_tuning_parameters[NDX_MIN_FREQUENCY_SMALL_PEAK] = (int)(my_controls.filter_hp_indel.min_allele_freq *100);
    peak_finding_tuning_parameters[NDX_MIN_FLOW_PEAK_SHORTHP_DISTANCE] = 85;
    peak_finding_tuning_parameters[NDX_MIN_FLOW_PEAK_LONGHP_DISTANCE] = 85;
    peak_finding_tuning_parameters[NDX_SHORT_HP_FOR_PEAK] = 8;
    peak_finding_tuning_parameters[NDX_MAX_PEAK_DEVIATION] = my_controls.control_peak.fpe_max_peak_deviation;
    peak_finding_tuning_parameters[NDX_PARAM_FIVE_NOT_USED] = 0;
    peak_finding_tuning_parameters[NDX_CALL_USING_EM_METHOD] = 0;
    peak_finding_tuning_parameters[NDX_PARAM_SEVEN_NOT_USED] = 0;
    peak_finding_tuning_parameters[NDX_STRAND_BIAS] = (int)(0.5*100);

    float * peak_finding_results = new float[13];

    int optimization_start, optimization_end;

    optimization_start = max((variant_identity.underCallLength-3)*100, 0);
    optimization_end = min((variant_identity.underCallLength+3)*100, MAXSIGDEV);
    int variation_allowed = variant_identity.underCallLength;

    runLMS((int*) flowDist->getHomPolyDist(), MAXSIGDEV, peak_finding_tuning_parameters, peak_finding_results,
           optimization_start, optimization_end,
           variant_identity.underCallLength+1, variation_allowed, DEBUG);




    compute_maxProb(peak_finding_results, variant_identity.underCallLength+1, maxProb, isUnderCallRef, refBias);

    underCallHPScore = compute_bayesian_score(maxProb);
    underCallFreq = peak_finding_results[5];

    delete[] peak_finding_results;

    //now evaluate the overcall HP length

    maxProb = 0;
    peak_finding_results = new float[13];
    for (size_t i = 0; i < 13; i++ )
      peak_finding_results[i] = 0;


    optimization_start = max((variant_identity.overCallLength-3)*100, 0);
    optimization_end = min((variant_identity.overCallLength+3)*100, MAXSIGDEV);
    variation_allowed = variant_identity.overCallLength;

    runLMS((int*) flowDist->getHomPolyDist(), MAXSIGDEV, peak_finding_tuning_parameters, peak_finding_results,
           optimization_start, optimization_end,
           variant_identity.overCallLength-1, variation_allowed, DEBUG);

    delete[] peak_finding_tuning_parameters;


    compute_maxProb(peak_finding_results, variant_identity.overCallLength-1, maxProb, isOverCallRef, refBias);

    overCallHPScore = compute_bayesian_score(maxProb);
    overCallFreq = peak_finding_results[5];

    //not sure how to move this part to decision tree, leaving it here for now.
    if (isUnderCallRef || isOverCallRef
        || underCallHPScore < 5 || overCallHPScore < 5
        || overCallFreq < my_controls.filter_snps.min_allele_freq || underCallFreq < my_controls.filter_snps.min_allele_freq) {
      //filter the variant as possible overcall undercall FP
      flowDist->summary_info.isFiltered = true;
      flowDist->summary_info.filterReason = "Overcall/Undercall_HP_SNP";
      flowDist->summary_info.alleleScore  = 0.0;
    }

  }

  stringstream infoss;


  infoss << "Score= " << scores[1] << " | STDBIAS= "<< stdBias;

  flowDist->summary_info.infoString = infoss.str();

  //InsertGenericInfoTag(candidate_variant, infoss);


  //if (filterReason!=NULL)
  //  delete filterReason;

  deleteArray(&scoreLikelihoods, totalReads, totalHypotheses);
}
void
ParticleSystem::_initialize(int numParticles)
{
    assert(!m_bInitialized);

    m_numParticles = numParticles;

    // allocate host storage
    m_hPos = new float[m_numParticles*4];
    m_hVel = new float[m_numParticles*4];
    memset(m_hPos, 0, m_numParticles*4*sizeof(float));
    memset(m_hVel, 0, m_numParticles*4*sizeof(float));

    m_hCellStart = new uint[m_numGridCells];
    memset(m_hCellStart, 0, m_numGridCells*sizeof(uint));

    m_hCellEnd = new uint[m_numGridCells];
    memset(m_hCellEnd, 0, m_numGridCells*sizeof(uint));

    // allocate GPU data
    unsigned int memSize = sizeof(float) * 4 * m_numParticles;

    if (m_bUseOpenGL)
    {
        m_posVbo = createVBO(memSize);
        registerGLBufferObject(m_posVbo, &m_cuda_posvbo_resource);
    }
    else
    {
        checkCudaErrors(cudaMalloc((void **)&m_cudaPosVBO, memSize)) ;
    }

    allocateArray((void **)&m_dVel, memSize);

    allocateArray((void **)&m_dSortedPos, memSize);
    allocateArray((void **)&m_dSortedVel, memSize);

    allocateArray((void **)&m_dGridParticleHash, m_numParticles*sizeof(uint));
    allocateArray((void **)&m_dGridParticleIndex, m_numParticles*sizeof(uint));

    allocateArray((void **)&m_dCellStart, m_numGridCells*sizeof(uint));
    allocateArray((void **)&m_dCellEnd, m_numGridCells*sizeof(uint));

    if (m_bUseOpenGL)
    {
        m_colorVBO = createVBO(m_numParticles*4*sizeof(float));
        registerGLBufferObject(m_colorVBO, &m_cuda_colorvbo_resource);

        // fill color buffer
        glBindBufferARB(GL_ARRAY_BUFFER, m_colorVBO);
        float *data = (float *) glMapBufferARB(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
        float *ptr = data;

        for (uint i=0; i<m_numParticles; i++)
        {
            float t = i / (float) m_numParticles;
#if 0
            *ptr++ = rand() / (float) RAND_MAX;
            *ptr++ = rand() / (float) RAND_MAX;
            *ptr++ = rand() / (float) RAND_MAX;
#else
            colorRamp(t, ptr);
            ptr+=3;
#endif
            *ptr++ = 1.0f;
        }

        glUnmapBufferARB(GL_ARRAY_BUFFER);
    }
    else
    {
        checkCudaErrors(cudaMalloc((void **)&m_cudaColorVBO, sizeof(float)*numParticles*4));
    }

    sdkCreateTimer(&m_timer);

    setParameters(&m_params);

    m_bInitialized = true;
}
Example #23
0
void CudaSystem::init()
{
    unsigned int maxParticles = 65536;
    unsigned int memSize = sizeof(double)*3*maxParticles;
    
    allocateArray((void**)&m_dVel[0], memSize);
    allocateArray((void**)&m_dVel[1], memSize);
    allocateArray((void**)&m_dPos[0], memSize);
    allocateArray((void**)&m_dPos[1], memSize);
    allocateArray((void**)&m_dMass, sizeof(double)*maxParticles);
    allocateArray((void**)&m_dParticleRadius, sizeof(double)*maxParticles);
    allocateArray((void**)&m_dInteractionRadius, sizeof(double)*maxParticles);
    allocateArray((void**)&m_dSpring, sizeof(double)*maxParticles);
    allocateArray((void**)&m_dDamping, sizeof(double)*maxParticles);
    allocateArray((void**)&m_dShear, sizeof(double)*maxParticles);
    allocateArray((void**)&m_dAttraction, sizeof(double)*maxParticles);

    m_hPos[0] = new double[maxParticles*3];
    m_hVel[0] = new double[maxParticles*3];
    m_hPos[1] = new double[maxParticles*3];
    m_hVel[1] = new double[maxParticles*3];
    m_hForce = new double[maxParticles*3];
    m_hColors = new double[maxParticles*4];   
    m_hMass = new double[maxParticles];
    m_hParticleRadius = new double[maxParticles];
    m_hInteractionRadius = new double[maxParticles];
    m_hSpring = new double[maxParticles];
    m_hDamping = new double[maxParticles];
    m_hShear = new double[maxParticles];
    m_hAttraction = new double[maxParticles];

    allocateArray((void**)&voisines.nbVoisines,maxParticles*sizeof(int));
    allocateArray((void**)&voisines.listeVoisine,maxParticles*200*sizeof(int));

    FExt->_initialize(maxParticles);
    gridCreated = false;

}
Example #24
0
void FluidSystem::_initialize(int numParticles)
{
    assert(!m_bInitialized);

    m_numParticles = numParticles;

    // allocate host storage
	m_hPos = new float[m_numParticles * 4];
	m_hVel = new float[m_numParticles * 4];
	m_hDen = new float[m_numParticles];//new just need 1 float to store density and pressure
	m_hPre = new float[m_numParticles];//new
	m_hColorf = new float[m_numParticles];//new
	buoyancyPos = new float[m_numParticles*3];

	memset(m_hPos, 0, m_numParticles * 4 * sizeof(float));
	memset(m_hVel, 0, m_numParticles * 4 * sizeof(float));
	memset(m_hDen, 0, m_numParticles * sizeof(float));//new just need 1 float to store density and pressure
	memset(m_hPre, 0, m_numParticles * sizeof(float));//new
	memset(m_hColorf, 0, m_numParticles * sizeof(float));//new
	memset(buoyancyPos, 0, m_numParticles*3 * sizeof(float));

	m_sortKeys.alloc(m_numParticles);               
	m_indices.alloc(m_numParticles, true, false, true);  //create as index buffer ,to sort

    m_hCellStart = new uint[m_numGridCells];
    memset(m_hCellStart, 0, m_numGridCells*sizeof(uint));

    m_hCellEnd = new uint[m_numGridCells];
    memset(m_hCellEnd, 0, m_numGridCells*sizeof(uint));

	// allocate GPU data
	unsigned int memSize = sizeof(float) * 4 * m_numParticles;
	unsigned int memSizetwo = sizeof(float) * m_numParticles;
	unsigned int memSizethree = sizeof(float) *3* m_numParticles;

	if (m_bUseOpenGL)
    {
        m_posVbo = createVBO(memSize);
		m_velVBO = createVBO(memSize);
		//obstaclePosVbo[0] = createVBO(memSize);
        registerGLBufferObject(m_posVbo, &m_cuda_posvbo_resource);
		registerGLBufferObject(m_velVBO, &m_cuda_velvbo_resource);
    }
    else
    {
        checkCudaErrors(cudaMalloc((void **)&m_cudaPosVBO, memSize)) ;
		checkCudaErrors(cudaMalloc((void **)&m_cudaVelVBO, memSize));
    }

    allocateArray((void **)&m_dVel, memSize);
	allocateArray((void **)&m_dDen, memSizetwo);
	allocateArray((void **)&m_dPre, memSizetwo);
	allocateArray((void **)&m_dColorf, memSizetwo);
	allocateArray((void **)&buoyancyForce, memSizethree);

    allocateArray((void **)&m_dSortedPos, memSize);
    allocateArray((void **)&m_dSortedVel, memSize);
	allocateArray((void **)&m_dSortedDen, memSizetwo);
	allocateArray((void **)&m_dSortedPre, memSizetwo);
	allocateArray((void **)&m_dSortedColorf, memSizetwo);

    allocateArray((void **)&m_dGridParticleHash, m_numParticles*sizeof(uint));
    allocateArray((void **)&m_dGridParticleIndex, m_numParticles*sizeof(uint));

    allocateArray((void **)&m_dCellStart, m_numGridCells*sizeof(uint));
    allocateArray((void **)&m_dCellEnd, m_numGridCells*sizeof(uint));

    if (m_bUseOpenGL)
    {
        //m_colorVBO = createVBO(m_numParticles*4*sizeof(float));
		m_colorVBO = createVBO(memSize);
        registerGLBufferObject(m_colorVBO, &m_cuda_colorvbo_resource);

        // fill color buffer
        glBindBufferARB(GL_ARRAY_BUFFER, m_colorVBO);
        float *data = (float *) glMapBufferARB(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
        float *ptr = data;

        for (uint i=0; i<m_numParticles; i++)
        {
			if (i < 450000){
				float t = i / (float)m_numParticles;

				colorRamp(0.1, 0.5, 0.7, ptr);
				ptr += 3;
				*ptr++ = 0.5f;
			}
			/*else if (250000 <= i&&i < 450000){
				float t = i / (float)m_numParticles;

				colorRamp(0, 1, 0, ptr);
				ptr += 3;
				*ptr++ = 0.5f;
			}*/
			else{
				float t = i / (float)m_numParticles;

				colorRamp(0, 0, 0, ptr);
				ptr += 3;
				*ptr++ = 1.0f;
			}
        }

        glUnmapBufferARB(GL_ARRAY_BUFFER);
    }
    else
    {
        checkCudaErrors(cudaMalloc((void **)&m_cudaColorVBO, sizeof(float)*numParticles*4));
    }

	plusBuoyancyforce(buoyancyForce, m_numParticles);//initialize this value to 0;

    sdkCreateTimer(&m_timer);

    setParameters(&m_params);

    m_bInitialized = true;
}
Example #25
0
void PoiseuilleFlowSystem::_initialize(int numParticles){
	assert(!IsInitialized);

	numParticles = numParticles;

	hPos = new float[numParticles*4];
	hVel = new float[numParticles*4];
	hVelLeapFrog = new float[numParticles*4];		
	hMeasures = new float[numParticles*4];
	hAcceleration = new float[numParticles*4];
	memset(hPos, 0, numParticles*4*sizeof(float));
	memset(hVel, 0, numParticles*4*sizeof(float));
	memset(hVelLeapFrog, 0, numParticles*4*sizeof(float));
	memset(hAcceleration, 0, numParticles*4*sizeof(float));	
	memset(hMeasures, 0, numParticles*4*sizeof(float)); 

	for(uint i = 0; i < numParticles; i++) //todo: check density approximation
		hMeasures[4*i+0] = params.restDensity;

	unsigned int memSize = sizeof(float) * 4 * numParticles;

	if (IsOpenGL) {
		posVbo = createVBO(memSize);    
	registerGLBufferObject(posVbo, &cuda_posvbo_resource);
	} else {
		checkCudaErrors( cudaMalloc( (void **)&cudaPosVBO, memSize )) ;
	}

	allocateArray((void**)&dVel, memSize);
	allocateArray((void**)&dVelLeapFrog, memSize);
	allocateArray((void**)&dAcceleration, memSize);
	allocateArray((void**)&dMeasures, memSize);

	allocateArray((void**)&dSortedPos, memSize);
	allocateArray((void**)&dSortedVel, memSize);
	
	allocateArray((void**)&dHash, numParticles*sizeof(uint));
	allocateArray((void**)&dIndex, numParticles*sizeof(uint));

	allocateArray((void**)&dCellStart, numGridCells*sizeof(uint));
	allocateArray((void**)&dCellEnd, numGridCells*sizeof(uint));

	if (IsOpenGL) {
		colorVBO = createVBO(numParticles*4*sizeof(float));
	registerGLBufferObject(colorVBO, &cuda_colorvbo_resource);

		// fill color buffer
		glBindBufferARB(GL_ARRAY_BUFFER, colorVBO);
		float *data = (float *) glMapBufferARB(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
		float *ptr = data;
		uint fluidParticles = params.fluidParticlesSize.x * params.fluidParticlesSize.y * params.fluidParticlesSize.z;
		for(uint i=0; i < numParticles; i++) {
			float t = 0.7f;  
			if(i < fluidParticles)
				t = 0.5f;  
			if(((i % params.gridSize.x) == 0) && i < fluidParticles)
				t = 0.2f;    			
			colorRamp(t, ptr);
			ptr+=3;
			*ptr++ = 1.0f;
		}
		glUnmapBufferARB(GL_ARRAY_BUFFER);
	} else {
		checkCudaErrors( cudaMalloc( (void **)&cudaColorVBO, sizeof(float)*numParticles*4) );
	}	   

	setParameters(&params);

	IsInitialized = true;
}
Example #26
0
void ParticleMesh::calculate(LevelsetCollider &LC, int size, float r, float mass)
 {
	
	float scale = 2 * r / (LC.gridStep*(LC.gridSize.x - 4) / size);
	vec3<double> center = vec3<double>(0, 0, 0);
	vec3<double> MaxB = LC.MaxBoundary;
	vec3<double> MinB = LC.MinBoundary;
	scale = CalScale(MaxB, MinB, r, size);
	int index_par = 0;
	par_list = new triple[20000];
	sdf_list = new SDF[20000];
	float step = r * 2 / scale;
	

	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size; j++)
		{
			for (int k = 0; k <size; k++)
			{
				vec3<int> gridPos = vec3<int>(i, j, k);
				vec3<double> pos = vec3<double>(i + 0.5, j + 0.5, k + 0.5) * (2 * r / scale) + MinB;
				int gridInd = dot(gridPos, LC.gridSizeOffset);
				double sdf = 0;
				LC.getGridDist(pos, sdf);
				if (sdf < 0 && sdf > -r * 2 / scale * 2)
				{
					vec3<double> pos_par = pos - MinB;// -vec3<double>(0.5, 0.5, 0.5) * (2 * r / scale);

					center += mass*pos_par * scale;
					par_list[index_par].x[0] = pos_par.x * scale;
					par_list[index_par].x[1] = pos_par.y * scale;
					par_list[index_par].x[2] = pos_par.z * scale;

					sdf_list[index_par].sdf = sdf * scale;
					vec3<double> gradient;
					LC.checkCollision(pos, sdf, gradient, -1);
					sdf_list[index_par].gradient[0] = gradient.x;
					sdf_list[index_par].gradient[1] = gradient.y;
					sdf_list[index_par].gradient[2] = gradient.z;
					index_par++;
				}
			}
		}
	}


	center /= index_par*mass;
	float3 center_f3 = make_float3(center[0], center[1], center[2]);
	m_totalParticles = index_par;
	m_totalConnectedPoints = LC.meshModel->m_totalConnectedPoints;
	m_totalFaces = LC.meshModel->m_totalFaces;
	float3 vertex,relative;
	mp_vertexXYZ = new float[m_totalConnectedPoints * 3];
	Relative_Vertex = new float3[m_totalConnectedPoints];
	mp_vertexNorm = new float[m_totalConnectedPoints * 3];
	mp_vertexRGB = new float[m_totalConnectedPoints * 3];
	for (int i = 0; i < m_totalConnectedPoints; i++)
	{
		vertex.x = LC.meshModel->mp_vertexXYZ[i * 3];
		vertex.y = LC.meshModel->mp_vertexXYZ[i * 3 + 1];
		vertex.z = LC.meshModel->mp_vertexXYZ[i * 3 + 2];
		//vertex *= scale;
		vertex -= make_float3(MinB[0], MinB[1], MinB[2]);// +make_float3(0.5, 0.5, 0.5) * (2 * r / scale);
		vertex *= scale;
		relative = vertex - center_f3;
		*(float3*)(mp_vertexXYZ + i * 3) = vertex;


		mp_vertexRGB[i * 3] = LC.meshModel->mp_vertexRGB[i * 3];
		mp_vertexRGB[i * 3 + 1] = LC.meshModel->mp_vertexRGB[i * 3 + 1];
		mp_vertexRGB[i * 3 + 2] = LC.meshModel->mp_vertexRGB[i * 3 + 2];

		mp_vertexNorm[i * 3] = 0; //LC.meshModel->mp_vertexNorm[i * 3];
		mp_vertexNorm[i * 3 + 1] = 0; //LC.meshModel->mp_vertexNorm[i * 3 + 1];
		mp_vertexNorm[i * 3 + 2] = 0;// LC.meshModel->mp_vertexNorm[i * 3 + 2];


		Relative_Vertex[i] = relative;
	}

	FaceVerInd = new uint[3 * m_totalFaces];
	//memcpy(FaceVerInd, LC.meshModel->mp_face, 9 * m_totalFaces * sizeof(uint));
	float3  n;
	for (int i = 0; i < m_totalFaces; i++)
	{
		uint i_1 = FaceVerInd[3 * i] = LC.meshModel->mp_face[3 * i];
		uint i_2 = FaceVerInd[3 * i + 1] = LC.meshModel->mp_face[3 * i + 1];
		uint i_3 = FaceVerInd[3 * i + 2] = LC.meshModel->mp_face[3 * i + 2];

		float3 v1 = make_float3(mp_vertexXYZ[i_1 * 3], mp_vertexXYZ[i_1 * 3 + 1], mp_vertexXYZ[i_1 * 3 + 2]);
		float3 v2 = make_float3(mp_vertexXYZ[i_2 * 3], mp_vertexXYZ[i_2 * 3 + 1], mp_vertexXYZ[i_2 * 3 + 2]);
		float3 v3 = make_float3(mp_vertexXYZ[i_3 * 3], mp_vertexXYZ[i_3 * 3 + 1], mp_vertexXYZ[i_3 * 3 + 2]);

		float3 e12 = v2 - v1;
		float3 e13 = v3 - v1;
		n = cross(e12, e13);
		n /= length(n);
		mp_vertexNorm[i_1 * 3] += n.x;
		mp_vertexNorm[i_1 * 3 + 1] += n.y;
		mp_vertexNorm[i_1 * 3 + 2] += n.z;

		//atomicAdd(&num_tri_per_point[i_1], 1);

		float3 e21 = -e12;
		float3 e23 = v3 - v2;
		n = cross(e23, e21);
		n /= length(n);
		mp_vertexNorm[i_2 * 3] += n.x;
		mp_vertexNorm[i_2 * 3 + 1] += n.y;
		mp_vertexNorm[i_2 * 3 + 2] += n.z;

		//atomicAdd(&num_tri_per_point[i_2], 1);

		float3 e31 = -e13;
		float3 e32 = -e23;
		n = cross(e31, e32);
		n /= length(n);
		mp_vertexNorm[i_3 * 3] += n.x;
		mp_vertexNorm[i_3 * 3 + 1] += n.y;
		mp_vertexNorm[i_3 * 3 + 2] += n.z;
	}
	for (int i = 0; i < m_totalConnectedPoints; i++)
	{
		

		float3 n = make_float3(mp_vertexNorm[i * 3], mp_vertexNorm[i * 3 + 1], mp_vertexNorm[i * 3 + 2]);

		mp_vertexNorm[i * 3] /= length(n); 
		mp_vertexNorm[i * 3 + 1] /= length(n);
		mp_vertexNorm[i * 3 + 2] /= length(n);
	}
	m_vertexVbo = createVBO(m_totalConnectedPoints * 3 * sizeof(float));
	registerGLBufferObject(m_vertexVbo, &m_cuda_vertexvbo_resource);

	m_indexVBO = createVBO(3 * m_totalFaces * sizeof(uint));
	registerGLBufferObject(m_indexVBO, &m_cuda_indexvbo_resource);

	m_colorVBO = createVBO(m_totalConnectedPoints * 3 * sizeof(float));
	registerGLBufferObject(m_colorVBO, &m_cuda_colorvbo_resource);

	m_NorVBO = createVBO(m_totalConnectedPoints * 3 * sizeof(float));
	registerGLBufferObject(m_NorVBO, &m_cuda_normalvbo_resource);

	unregisterGLBufferObject(m_cuda_vertexvbo_resource);
	glBindBuffer(GL_ARRAY_BUFFER, m_vertexVbo);
	glBufferSubData(GL_ARRAY_BUFFER, 0, m_totalConnectedPoints * 3 * sizeof(float), mp_vertexXYZ);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	registerGLBufferObject(m_vertexVbo, &m_cuda_vertexvbo_resource);

	unregisterGLBufferObject(m_cuda_indexvbo_resource);
	glBindBuffer(GL_ARRAY_BUFFER, m_indexVBO);
	glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * m_totalFaces * sizeof(uint), FaceVerInd);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	registerGLBufferObject(m_indexVBO, &m_cuda_indexvbo_resource);

	unregisterGLBufferObject(m_cuda_colorvbo_resource);
	glBindBuffer(GL_ARRAY_BUFFER, m_colorVBO);
	glBufferSubData(GL_ARRAY_BUFFER, 0, m_totalConnectedPoints * 3 * sizeof(float), mp_vertexRGB);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	registerGLBufferObject(m_colorVBO, &m_cuda_colorvbo_resource);



	unregisterGLBufferObject(m_cuda_normalvbo_resource);
	glBindBuffer(GL_ARRAY_BUFFER, m_NorVBO);
	glBufferSubData(GL_ARRAY_BUFFER, 0, m_totalConnectedPoints * 3 * sizeof(float), mp_vertexNorm);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	registerGLBufferObject(m_NorVBO, &m_cuda_normalvbo_resource);

	allocateArray((void**)&dRelative_Vertex, m_totalConnectedPoints * 3 * sizeof(float));
	copyArrayToDevice(dRelative_Vertex, Relative_Vertex, 0, m_totalConnectedPoints * 3 * sizeof(float));
	hasMesh = true;
}
/* create the vertices array: vertex */
void createGeometry(int type, Geometry* geo, int cols, int rows)
{
#ifdef DEBUG_GEOMETRY
  fprintf(stderr, "new geometry %i %i\n", cols, rows);
#endif

  geo->rows = rows;
  geo->cols = cols;
  allocateArray(geo);

  float u = 0.0;
  float v = 0.0;

  int i , j, index;

    float gridStep = GRID_LENGTH/cols;
    float start = -1.0;
    index = 0;

    switch (type) {
        case SHAPE_IMM_GRID:
        case SHAPE_GRID:
          for(j = 0; j <= cols; j++)
          {
            for(i = 0; i <= rows ; i++)
            {
              geo->vertex[index].x = start + gridStep * i;
              geo->vertex[index].y = start + gridStep * j;
              geo->vertex[index].z = 0;
              index++;
            }
          }
      break;
      case SHAPE_IMM_SPHERE:
      case SHAPE_SPHERE:
          for(j = 0; j <= cols; j++)
          {
              v =  M_PI * j / rows;
            for(i = 0; i <= rows; i++)
            {
              u = 2.0*M_PI * i / rows;
              geo->vertex[index].x = 1.0 * cos(u) * sin(v);
              geo->vertex[index].y = 1.0 * sin(u) * sin(v);
              geo->vertex[index].z = 1.0 * cos(v);
              index++;
            }
          }
      break;
      case SHAPE_IMM_TORUS:
      case SHAPE_TORUS:
          for(j = 0; j <= cols; j++)
          {
              u =  2.0 * M_PI / rows * j ;
            for(i = 0; i <= rows; i++)
            {
              v = 2.0*M_PI / rows * i ;

              geo->vertex[index].x = (TORUS_EX_RADIUS + TORUS_IN_RADIUS *
                                      cos(u)) * cos(v);
              geo->vertex[index].y = (TORUS_EX_RADIUS + TORUS_IN_RADIUS *
                                      cos(u)) * sin(v);
              geo->vertex[index].z = TORUS_IN_RADIUS * sin(u);
              index++;
            }
          }
      break;
      case SHAPE_INNER_GRID:
          uInnerShapeType = GPU_GRID;
          createInnerGeometry(geo,cols,rows);
      break;
      case SHAPE_INNER_SHPERE:
          uInnerShapeType = GPU_SPHERE;
          createInnerGeometry(geo,cols,rows);
      break;
      case SHAPE_INNER_TORUS:
          uInnerShapeType = GPU_TORUS;
          createInnerGeometry(geo,cols,rows);
      break;

    }
}
Example #28
0
BBArray *bbArraySlice( const char *type,BBArray *inarr,int beg,int end ){

	char *p;
	void *init;
	BBArray *arr;
	int n,k,el_size;
	int length=end-beg;

	if( length<=0 ) return &bbEmptyArray;
	
	arr=allocateArray( type,1,&length );

	el_size=arr->size/length;
	
	init=arrayInitializer( arr );
	p=(char*)BBARRAYDATA( arr,1 );

	n=-beg;
	if( n>0 ){
		if( beg+n>end ) n=end-beg;
		if( init ){
			void **dst=(void**)p;
			for( k=0;k<n;++k ) *dst++=init;
			p=(char*)dst;
		}else{
			memset( p,0,n*el_size );
			p+=n*el_size;
		}
		beg+=n;
		if( beg==end ) return arr;
	}
	n=inarr->scales[0]-beg;
	if( n>0 ){
		if( beg+n>end ) n=end-beg;
#ifdef BB_GC_RC
		if( type[0]==':' || type[0]=='$' || type[0]=='[' ){
			BBObject **dst=(BBObject**)p;
			BBObject **src=(BBObject**)BBARRAYDATA(inarr,inarr->dims)+beg;
			for( k=0;k<n;++k ){ 
				BBObject *o=*src++;
				BBINCREFS( o );
				*dst++=o; 
			}
			p=(char*)dst;
		}else{
			memcpy( p,(char*)BBARRAYDATA(inarr,inarr->dims)+beg*el_size,n*el_size );
			p+=n*el_size;
		}
#else
		memcpy( p,(char*)BBARRAYDATA(inarr,inarr->dims)+beg*el_size,n*el_size );
		p+=n*el_size;
#endif
		beg+=n;
		if( beg==end ) return arr;
	}
	n=end-beg;
	if( n>0 ){
		if( init ){
			void **dst=(void**)p;
			for( k=0;k<n;++k ) *dst++=init;
		}else{
			memset( p,0,n*el_size );
		}
	}
	return arr;
}