bool PageNetTest::stop() {
	m_running    = false;
	m_runNetTest = false;
	//reset();
	m_numResultsSent = 0;
	m_numResultsRecv  = 0;
	if( m_hostId == 0 ) collectResults();
	return true;
}
示例#2
0
// funky recursive algorithm for collecting derivative results. Best illustrated by examples:
// ordering for bivariate second derivatives:  1, dx,dy, d2x,dxdy,d2y
// ordering for trivariate second derivatives: 1, dx,dy,dz, d2x,dxdy,dxdz,d2y,dydz,d2z
// ordering for trivariate third derivatives:  1,
//                                            dx,dy,dz,
//                                            d2x,dxdy,dxdz,d2y,dydz,d2z,
//                                            d3x,d2xdy,d2xdz,dxd2y,dxdydz,dxd2z,d3y,d2ydz,dyd2z,d3z
void collectResults(std::vector<double>::iterator &result,
                    double product,
                    std::vector<std::vector<std::vector<double> > > &diff,
                    int derivsLeft,
                    uint dim) {
	if(dim == diff.size()-1) {
		*result *= product*diff.back()[derivsLeft][0];
		result++;
		return;
	}
	for(int d=derivsLeft; d>-1; d--) {
		double ans = diff[dim][d][0];
		collectResults(result, ans*product, diff, derivsLeft-d, dim+1);
	}
};
/*!
  \internal

  Both solveMin and solveMax are interfaces to this method.

  The enum solverFactor admits 2 values: Minimum (-1) and Maximum (+1).

  This method sets the original objective and runs the second phase
  Simplex to obtain the optimal solution for the problem. As the internal
  simplex solver is only able to _maximize_ objectives, we handle the
  minimization case by inverting the original objective and then
  maximizing it.
*/
qreal QSimplex::solver(solverFactor factor)
{
    // Remove old objective
    clearRow(0);

    // Set new objective in the first row of the simplex matrix
    qreal resultOffset = 0;
    QHash<QSimplexVariable *, qreal>::const_iterator iter;
    for (iter = objective->variables.constBegin();
         iter != objective->variables.constEnd();
         ++iter) {

        // Check if the variable was removed in the simplification process.
        // If so, we save its offset to the objective function and skip adding
        // it to the matrix.
        if (iter.key()->index == -1) {
            resultOffset += iter.value() * iter.key()->result;
            continue;
        }

        setValueAt(0, iter.key()->index, -1 * factor * iter.value());
    }

    solveMaxHelper();
    collectResults();

#ifdef QT_DEBUG
    for (int i = 0; i < constraints.size(); ++i) {
        Q_ASSERT(constraints[i]->isSatisfied());
    }
#endif

    // Return the value calculated by the simplex plus the value of the
    // fixed variables.
    return (factor * valueAt(0, columns - 1)) + resultOffset;
}
/* MAIN BEGINS */
int main(){

	/* TEMP : WELCOME MESSAGE AND ASKING FOR USER DEFINED NUMDER OF THREADS*/
	printf("=============================WELCOME============================\n");
	printf("            LAB ASSIGNMENT 2B (Row-wise Dynamic Scheduling      \n");
	printf("================================================================\n\n");
	printf("Enter Number of Threads: ");
	scanf("%d", &numThreads);

	/* Check for multiplication compatibility */
		if (COL_A != ROW_B){
			printf("Column for Matrix A should be same as that of Row for Matrix B\n");
			printf("Multiplication is not possible\n");
			return(0); //int main is used to control the untimely exit
		}

		/* Allocate Memory for the Matrix and add Random Double values into it */
		allocateMemory();

		/* Generate Random numbers and fill them in the Matrix*/
		fillMatrix();

		/* Parallel Multiplication*/
		parallelMultiplication();

		//free all of the memory
	  printf("Freeing memory.....\n");
	  free(matA);
	  free(matB);
	  free(matC);

	/* Output FILE HANDLING */
	  collectResults();

  return 0;
} //end main()
示例#5
0
文件: ppu.c 项目: kenkendk/dsmcbe
void canon(int id, int shots, int shots_spu, int canonX, int canonY, float canonAX, float canonAY, unsigned char* energy)
{
	int shotsspu = SHOTS_SPU;
	unsigned int i;
	unsigned long size;

	struct PACKAGE* package;
	package = dsmcbe_create(JOB+id, sizeof(struct PACKAGE));
	package->id = 0;
	package->maxid = (shots / shots_spu);
	package->heigth = HEIGTH;
	package->width = WIDTH;
	package->shots_spu = shotsspu;

#ifdef STATIC	
	package->tot_shots_spu =  ceil((SHOTS / SHOTS_SPU) / (double)(SPU_THREADS * MAX(dsmcbe_MachineCount(), 1)));
#else
	package->tot_shots_spu =  SPU_THREADS * MAX(dsmcbe_MachineCount(), 1);
#endif	

	package->canonX = canonX;
	package->canonY = canonY;
	package->canonAX = canonAX;
	package->canonAY = canonAY;
	dsmcbe_release(package);
	
	collectResults(id, shots, shots_spu, energy);	

	for(i = 0; i < SPU_THREADS * MAX(dsmcbe_MachineCount(), 1); i++)
	{
		//printf("Reading FINISH package with id %i\n", FINISHED + (id * 100) + i);
		dsmcbe_release(dsmcbe_acquire(FINISHED+(id*100)+i, &size, ACQUIRE_MODE_READ));
		//printf("Read FINISH package with id %i\n", FINISHED + (id * 100) + i);		
	}

}
示例#6
0
/************************************************************************************************************************//**
 * \brief evaluates a general B-spline (currently only bivariate and trivariate supported - small fix to extend, but not now)
 * \param results [out] Vector of all results
 * \param parPt Parametric evaluation point 
 * \param derivs Number of derivatives requested
 * \param from_right Vector of same size as parPt stating if any of the parametric directions should be evaluated in the limit from
 *                   the right
 * 
 * Upon function return, results contains either (derivs+1)*(derivs+2)/2 (bivariate) or (derivs+1)*(derivs+2)*(2*derivs+6)/12 (trivariate)
 * evaluation points. These are all derivatives and organized as follows:
 * Bivariate splines up to second order: 1,dx,dy,d2x,dxdy,d2y,...
 * Trivariate splines up to second order: 1, dx,dy,dz, d2x,dxdy,dxdz,d2y,dydz,d2z
 * Trivariate splines up to third order: 1, dx,dy,dz, d2x,dxdy,dxdz,d2y,dydz,d2z, d3x,d2xdy,d2xdz,dxd2y,dxdydz,dxd2z,d3y,d2ydz,dyd2z,d3z
 ***************************************************************************************************************************/
void Basisfunction::evaluate(std::vector<double> &results, const std::vector<double> &parPt, int derivs, const std::vector<bool> &from_right) const {
	uint dim = knots_.size();
	if(dim != parPt.size() || dim != from_right.size()) {
		std::cerr << "Error Basisfunction::evalate(...) parametric dimension mismatch" << std::endl;
		exit(9230);
	}

	if(dim == 2) {    // bivariate splines
		results.resize((derivs+1)*(derivs+2)/2);               // (this is the triangular numbers)
	} else if(dim == 3) { // trivariate splines
		results.resize((derivs+1)*(derivs+2)*(2*derivs+6)/12); // (sum of triangular numbers)
	} else {
		std::cerr << "Error Basisfunction::evalate(...) for parametric dimension other than 2 or 3" << std::endl;
		exit(9231);
	}
	fill(results.begin(), results.end(), 0.0);

	std::vector<std::vector<double> >               ans(dim);
	std::vector<std::vector<std::vector<double> > > diff(dim);
	uint i = 0;
	for(std::vector<double> knot : knots_) {
		if(knot[0] > parPt[i] || parPt[i] > knot.back())
			return;
		ans[i].resize(knot.size()-1);
		for(uint j=0; j<knot.size()-1; j++) {
			if(from_right[i])
				ans[i][j] = (knot[j] <= parPt[i] && parPt[i] <  knot[j+1]) ? 1 : 0;
			else
				ans[i][j] = (knot[j] <  parPt[i] && parPt[i] <= knot[j+1]) ? 1 : 0;
		}

		diff[i].resize(derivs+1);
		int diff_level = knot.size()-2;
		for(uint n=1; n<knot.size()-1; n++, diff_level--) {
			if(diff_level <= derivs) {
				diff[i][diff_level].resize(diff_level+1);
				for(int j=0; j<=diff_level; j++)
					diff[i][diff_level][j] = ans[i][j];
			}
			for(int d = diff_level; d<= derivs; d++) {
				for(uint j=0; j<knot.size()-1-n; j++) {
					diff[i][d][j]  = (knot[ j+n ]==knot[ j ]) ? 0 : (   n   )/(knot[j+n]  -knot[ j ])*diff[i][d][ j ];
					diff[i][d][j] -= (knot[j+n+1]==knot[j+1]) ? 0 : (   n   )/(knot[j+n+1]-knot[j+1])*diff[i][d][j+1];
				}
			}
			for(uint j=0; j<knot.size()-1-n; j++) {
				ans[i][j]  = (knot[ j+n ]==knot[ j ]) ? 0 : (  parPt[i]-knot[j]  )/(knot[j+n]  -knot[ j ])*ans[i][ j ];
				ans[i][j] += (knot[j+n+1]==knot[j+1]) ? 0 : (knot[j+n+1]-parPt[i])/(knot[j+n+1]-knot[j+1])*ans[i][j+1];
			}
		}

		i++;
	}
			
	// collect results
	for(i=0; i<dim; i++)
		diff[i][0] = ans[i];

	fill(results.begin(), results.end(), weight_);
	std::vector<double>::iterator resIt = results.begin();
	for(int totDeriv=0; totDeriv<=derivs; totDeriv++)
		collectResults(resIt, 1.0, diff, totDeriv, 0);
}
bool PageNetTest::gotResults( TcpSocket *s ) {
	char *buf;
	long  bufLen, bufMaxLen;
	HttpMime mime;

	if ( g_errno ) {
		log( "net: nettest: g_errno: %s", mstrerror(g_errno) );
		g_errno = 0;
		return false;
	}
	if ( !s ) return false;


	buf       = s->m_readBuf;
	bufLen    = s->m_readOffset;
	bufMaxLen = s->m_readBufSize;

	char temp[64];
	long len = 0;
	len = sprintf(temp, "http://%s:%i/get?rnettest=1", 
		      iptoa(s->m_ip), s->m_port);
	Url u;
	u.set( temp, len );
	if ( !mime.set ( buf, bufLen, &u ) ) {		
		log( "net: nettest: MIME.set() failed." );
		return false;
	}

	if ( mime.getHttpStatus() != 200 ) {
		log( "net: nettest: MIME.getHttpStatus() failed." );
	        return false;
	}

	long state = 0;
	long hostId = 0;
	long testId = 0;

	if( !bufLen ) log( LOG_INFO, "net: nettest: we got an empty doc." );

	buf += mime.getMimeLen();
	bufLen -= mime.getMimeLen();

	for( long i = 0; i < bufLen; i++ ){		
		if( buf[i] == ' '  ) continue;
		if( buf[i] == '\r' ) continue;
		if( buf[i] == '\n' ) continue;
		if( buf[i] <  '0'  ) continue;

		if( state == 0 ) {
			hostId = atoi(&buf[i]);
			log( LOG_DEBUG, "net: nettest: host id is %ld",
			     hostId);
			state = 1;
		}
		else if( state == 1 ) {
			testId = atoi(&buf[i]);
			log( LOG_DEBUG, "net: nettest: test id is %ld",
			     testId);
			state = 2;
		}
		else if( state == 2 ){
			if( ((testId < hostId) || !hostId) && (testId) ) {
				if( !m_hostRates[0][hostId] )
					m_hostRates[0][hostId] = atoi(&buf[i]);
				else 
					m_hostRates[2][hostId] = atoi(&buf[i]);
			}
			else {
				if( !m_hostRates[2][hostId] )
					m_hostRates[2][hostId] = atoi(&buf[i]);
				else
					m_hostRates[0][hostId] = atoi(&buf[i]);
			}
			state = 3;
			log( LOG_DEBUG, "net: nettest: send rate is %d",
			     atoi(&buf[i]));
		}
		else if( state == 3 ) {
			if( ((testId < hostId) || !hostId) && (testId) ) {
				if( !m_hostRates[1][hostId] )
					m_hostRates[1][hostId] = atoi(&buf[i]);
				else
					m_hostRates[3][hostId] = atoi(&buf[i]);
			}
			else {
				if( !m_hostRates[3][hostId] )
					m_hostRates[3][hostId] = atoi(&buf[i]);
				else
					m_hostRates[1][hostId] = atoi(&buf[i]);				
			}
			state = 0;
			log( LOG_DEBUG, "net: nettest: rcv rate is %d",
			     atoi(&buf[i]));
		}

		while( buf[i+1] >= '0'  ) i++;
	}


	if( m_numResultsSent < g_hostdb.getNumHosts() )
		return collectResults();

	if( ++m_numResultsRecv < m_numResultsSent )
		return false;
	
	return true;
}
//@Override
void AbstractDelegatingMasterTask::execute() {
    prepare();
    waitForResults();
    collectResults();
}
示例#9
0
文件: ppu.c 项目: kenkendk/dsmcbe
int main(int argc, char* argv[])
{

	char* input = NULL;
	char* output = NULL;	

	if(argc == 6) {
		input = argv[1];
		output = argv[2];
		SPU_THREADS = atoi(argv[3]);
		PPEid = atoi(argv[4]);
		file = argv[5]; 	
	} else if (argc == 4) {
		PPEid = 0;
		file = NULL; 		 		
		input = argv[1];
		output = argv[2]; 	
		SPU_THREADS = atoi(argv[3]);
	} else if (argc == 5) {
		PPEid = 0;
		file = NULL; 		 		
		input = argv[1];
		output = argv[2]; 	
		SPU_THREADS = atoi(argv[3]);
		
	} else {
		printf("Wrong number of arguments %i\n", argc);
		return -1;
	}
	
	pthread_t* threads;	
	struct IMAGE_FORMAT result;
	unsigned char* energy;
	unsigned char* cmap;
	unsigned char* scale;
	int scale_size = 9;
	char timer_buffer[256];
	int y, x;
	size_t i;

	WIDTH = 576;
	HEIGTH = 708;
	
	threads = dsmcbe_simpleInitialize(PPEid, file, SPU_THREADS);


	unsigned int* speIDs = dsmcbe_create(SPEID + PPEid, 4);
	*speIDs = PPEid * SPU_THREADS;
	dsmcbe_release(speIDs);

	if (PPEid == 0)
	{
		//printf("Starting loading images!\n");	
		loadImageNormal();
		//loadImageSmall();
		//printf("Finished loading images!\n");
		
			
		//for(i = 0; i < ROUNDS; i++)
		//{
			//struct POINTS* points = create(RESULT + i, sizeof(struct POINTS) * SHOTS_SPU);
			//memset(points, 0, sizeof(struct POINTS) * SHOTS_SPU);
			//release(points);	
		//}
	
			
		energy = dsmcbe_create(ENERGY+PPEid, (sizeof(unsigned char) * (HEIGTH * WIDTH)));
		memset(energy, 0, sizeof(unsigned char) * (HEIGTH * WIDTH));
	
		cmap = (unsigned char*)malloc(sizeof(unsigned char)*(9*3));
		cmap[0] = 0; cmap[1] = 0; cmap[2] = 85;
		cmap[3] = 0; cmap[4] = 0; cmap[5] = 170;
		cmap[6] = 0; cmap[7] = 0; cmap[8] = 255;
		cmap[9] = 0; cmap[10] = 85; cmap[11] = 0;
		cmap[12] = 0; cmap[13] = 170; cmap[14] = 0;
		cmap[15] = 0; cmap[16] = 255; cmap[17] = 0;
		cmap[18] = 85; cmap[19] = 0; cmap[20] = 0;
		cmap[21] = 170; cmap[22] = 0; cmap[23] = 0;
		cmap[24] = 255; cmap[25] = 0; cmap[26] = 0;
	
		scale = (unsigned char*)malloc(sizeof(unsigned char)*9);
		scale[0] = 10; scale[1] = 20; scale[2] = 30;
		scale[3] = 40; scale[4] = 50; scale[5] = 60;
		scale[6] = 70; scale[7] = 80; scale[8] = 90;
		
		srand(1);
	
		//Start timer!
		sw_init();
		sw_start();
				
		printf("Timer started\n");
	
		printf("Start firering canon #1\n");
		canon(0, SHOTS, SHOTS_SPU, 85, 75, 1.0, 0.8, energy);
		printf("Stopped firering canon #1\n");
	
		printf("Start firering canon #2\n");
		canon(1, SHOTS, SHOTS_SPU, 10, 230, 1.0, 0.0, energy);
		printf("Stopped firering canon #2\n");
	
		printf("Start firering canon #3\n");
		canon(2, SHOTS, SHOTS_SPU, 550, 230, -1.0, 0.0, energy);
		printf("Stopped firering canon #3\n");
	
		printf("Start firering canon #4\n");
		canon(3, SHOTS, SHOTS_SPU, 475, 90, -1.0, 0.75, energy);
		printf("Stopped firering canon #4\n");
		
		printf("Start firering canon #5\n");
		canon(4, SHOTS, SHOTS_SPU, 280, 0, 0.0, 1.0, energy);
		printf("Stopped firering canon #5\n");
			
		// Stop timer!
		sw_stop();
		sw_timeString(timer_buffer);
		printf("Time used: %s\n", timer_buffer);
	
		readimage_rgb(input, malloc, &result);
		unsigned long size;

		//printf("Starting harvest\n");		
		for(i=1; i<MAX(dsmcbe_MachineCount(), 1); i++)
		{
			//printf("START - FINISHJOB %i\n", FINISHJOB+i);
			dsmcbe_release(dsmcbe_acquire(FINISHJOB+i, &size, ACQUIRE_MODE_READ));
			//printf("END - FINISHJOB %i\n", FINISHJOB+i);
			//printf("START - ENERGY %i\n", ENERGY+i);
			unsigned char* temp = dsmcbe_acquire(ENERGY+i,&size, ACQUIRE_MODE_READ);

			for(y=0; y<HEIGTH; y++)
			{
				for(x=0; x<WIDTH; x++)
				{
					energy[MAPOFFSET(x,y)] = temp[MAPOFFSET(x,y)];
				}
			}
			dsmcbe_release(temp);
			//printf("END - ENERGY %i\n", ENERGY+i);
		}
		
		//printf("Harvest done\n");

		// Save energy map to image
		for(y=0; y<HEIGTH; y++)
		{
			for(x=0; x<WIDTH; x++)
			{
				if(energy[MAPOFFSET(x,y)] > 0)
				{
					int offset = 3 * fpos(scale, scale_size, energy[MAPOFFSET(x,y)]);
					result.image[MAPOFFSET(x,y)].r = cmap[offset];
					result.image[MAPOFFSET(x,y)].g = cmap[offset+1];
					result.image[MAPOFFSET(x,y)].b = cmap[offset+2];
				}
			}			
		}
		
		//printf("Done\n");

		dsmcbe_release(energy);
		writeimage_rgb(output, &result);
	
		free(cmap);
		free(scale);
		free(result.image);
	}

	if (PPEid != 0)
	{	
		energy = dsmcbe_create(ENERGY+PPEid, sizeof(unsigned char) * (708 * 576));
		memset(energy, 0, sizeof(unsigned char) * (708 * 576));
		
		collectResults(0, SHOTS, SHOTS_SPU, energy);
		collectResults(1, SHOTS, SHOTS_SPU, energy);
		collectResults(2, SHOTS, SHOTS_SPU, energy);
		collectResults(3, SHOTS, SHOTS_SPU, energy);
		collectResults(4, SHOTS, SHOTS_SPU, energy);
		
		dsmcbe_release(energy);
		
		//printf("CREATING - FINISHJOB %i\n", FINISHJOB+PPEid);
		dsmcbe_release(dsmcbe_create(FINISHJOB+PPEid, sizeof(unsigned int)));
		//printf("CREATED\n");
		
		for(i = 0; i < SPU_THREADS; i++)
			pthread_join(threads[i], NULL);
	}
	
	printf("Going to sleep before we die\n");
	//sleep(10);
	return 0;
}