void updateTreeNode(int x)
{
    if(node[x].update)
    {
        node[left(x)].sum += node[x].update * nodeLength(left(x));
        node[left(x)].update += node[x].update;
        node[right(x)].sum += node[x].update * nodeLength(right(x));
        node[right(x)].update += node[x].update;
        node[x].update = 0;
    }
}
void updateTree(int x, int l, int r, int v)
{
    if(node[x].l == l && node[x].r == r)
    {
        node[x].sum += v * nodeLength(x);
        node[x].update += v;
    }
    else
    {
        updateTreeNode(x);
        int mid = midium(node[x].l, node[x].r);
        if(r <= mid)
        {
            updateTree(left(x), l, r, v);
        }
        else if(l > mid)
        {
            updateTree(right(x), l, r, v);
        }
        else
        {
            updateTree(left(x), l, mid, v);
            updateTree(right(x), mid + 1, r, v);
        }
        node[x].sum = node[left(x)].sum + node[right(x)].sum;
    }
}
Exemple #3
0
/****************************************************************
*
*	Function: main
*	Input:	int argc 	number of command line arguements
*		char **arg	pointer to those arguements
*
*	Output: int 0 for success and 1 for error
*
*	Description: Runs the simple merge algorithm multiple
*	times averaging the results and printing them to terminal. 
*
*****************************************************************/
int main(int argc, char **argv)
{
	struct timeval startt, endt, result;
	
	char name[8] = "posix/";
	
	int status = 0;
	int n;
	int* S;
	int* R;
	int* P;
	
	//Check if app was given enough input
	if(argc < 6){
		printf("Missing Arguement Parameters\n");
		printf("Format ./seq path_input input_size ans_Path RUNS MAX_THREADS\n");
		return 1;
	}
	
	//Save args to memory and allocate memory for arrays
	n = atoi(argv[2])+1;
	RUNS = atoi(argv[4]);
	MAX_THREADS = atoi(argv[5]);
	S = malloc(n*sizeof(int));
	R = malloc(n*sizeof(int));
	P = malloc(n*sizeof(int));	

	if(S==NULL){
		printf("Failed to Allocate Memory for Input Array S");	
	}
	if(R==NULL){
		printf("Failed to Allocate Memory for Input Array R");	
	}

	//Read the input array from file and save to memory
	status = read_input(S, n, argv[1]);

	if(status){	
		printf("Failed to Read Input S\n");
		return 1;
	}
	
	/*Declaring Temporary Arrays*/
	int *P_temp = malloc(n*sizeof(int));	
	int *R_temp = malloc(n*sizeof(int));

	//Start of testing of the algorithm
	int j, i;
	double average;
	for(j=0; j<RUNS; j++){
		memset(R, 0, n*sizeof(int));
		memset(P, 0, n*sizeof(int));

		/*Start Timer*/
		result.tv_sec=0;
		result.tv_usec=0;
		gettimeofday (&startt, NULL);
	
		int rc;
		pthread_t thread_id[MAX_THREADS];
		pthread_attr_t attr[MAX_THREADS];

		int k;
		for(k=0; k<MAX_THREADS; k++){
			pthread_attr_init(&attr[k]);
			pthread_attr_setdetachstate(&attr[k], PTHREAD_CREATE_DETACHED);
		}

		//Initiate the barrier variable for threads
		pthread_barrier_init (&barrier, NULL, MAX_THREADS);
		pthread_barrier_init (&init, NULL, MAX_THREADS);

		/*Setup variables to be passed to threads*/
		args input[MAX_THREADS];
		for(k=0; k<MAX_THREADS; k++){
			input[k].S = S;
			input[k].R = R;
			input[k].P = P;
			input[k].n = n;
			input[k].id = k;
			input[k].R_temp = R_temp;
			input[k].P_temp = P_temp;
		}

		/*Initialize the Array in Parrallel*/

		jobs.queue1[0] = 0;
		jobs.queue1[1] = n;
		jobs.m = ceil(log2(n)); /*Set the number of steps*/
		
		for(k=1; k<MAX_THREADS; k++){
			pthread_create(&thread_id[k], &attr[k], &arrayInit, &input[k]);
		}
		arrayInit(&input[0]);

		/*Compute the Distance of each Node*/

		jobs.queue1[0] = 0;
		jobs.queue1[1] = n;
		
		for(k=1; k<MAX_THREADS; k++){
			pthread_create(&thread_id[k], &attr[k], &nodeLength, &input[k]);
		}
		nodeLength(&input[0]);

		/*Stop Timer*/
		gettimeofday (&endt, NULL);
		result.tv_usec = (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
		average += result.tv_usec;

		//Release barrier resources
		pthread_barrier_destroy(&barrier);
		pthread_barrier_destroy(&init);
		
		
	}
	average = average/RUNS; //Average the execution times

	//print results to terminal
	printf("%d 	%f	us \n",n-1,average);

	if(atoi(argv[3])!=1)
	{
		status = outputCheck(R, argv[3], n);
		if(status){
			printf("Incorrect Answer\n");
		}
		else{
			printf("Correct Answer\n");
		}
	}
	
	/*Save the Results if the output is less than 50 elements*/
	if(n<=50){
		status = write_output(S, R, n, name);
	}

	if(status){	
		printf("Failed to Write Output \n");
		return 1;
	}
	
	free(S);
	free(R);
	free(P);
	free(R_temp);
	free(P_temp);	
	
    	return 0;
}
Exemple #4
0
/****************************************************************
*
*	Function: nodeLength
*	Input:	int *S	Pointer to Input Array S
*		int *R	Pointer to Output Array R
*		int n   Size of the Arrays S and R
*
*	Output: returns nothing
*
*	Description: Takes a linked list array S and finds the
*	distance of each node in S to the final node 0. The 
*	distance is saved in the output array R. 
*
*****************************************************************/
void* nodeLength(void* argv){
	int i;
	int *S,*P,*R, *R_temp, *P_temp;

	args* input = (args*)argv;
	S = input->S;
	P = input->P;
	R = input->R;
	R_temp = input->R_temp;
	P_temp = input->P_temp;

	int status;
	status = checkQueue(input);
	
	if(status>0){

		/*Process each node */
		for(i=(input->s1); i<(input->e1); i++){
			if(P[i] > 0){	
				R_temp[i] = R[i]+R[P[i]];			
				P_temp[i] = P[P[i]];
			}	
		}

		/*Check to see if more work is left*/
		nodeLength(input);
	}
	else{	
		/*Wait for other threads to finish step*/
		pthread_barrier_wait(&barrier);

		/*Reset Work Queue so we can start Copying Results Back*/
		if(input->id == 0){ /*Only master touches it*/
			jobs.queue1[0] = 0;
			jobs.queue1[1] = input->n;
		}

		/*Wait for Master to update the Queue*/
		pthread_barrier_wait(&barrier);

		/*Start Copying array back*/		
		arrayCopy(input);

		/*Wait for other threads to finish*/
		pthread_barrier_wait(&barrier);

		/*Finished all steps*/
		if(status==-1){
			if(input->id){		
				pthread_exit(NULL);
			}
		}
		else{
			/*Reset the work queue*/
			if(input->id == 0){ /*Only master touches it*/
				jobs.m -= 1;		
				jobs.queue1[0] = 0;
				jobs.queue1[1] = input->n;
			}
			/*Wait for other threads to finish step*/
			pthread_barrier_wait(&barrier);
			nodeLength(input);
		}
	}

}
 int nodeLength(TreeNode* root, TreeNode* parent, int len){
     if(!root) return len;
     len = (parent && root->val==parent->val+1) ? len+1 : 1;
     return max(len,
         max(nodeLength(root->left,root,len),nodeLength(root->right,root,len)));
 }
 int longestConsecutive(TreeNode* root) {
     return nodeLength(root, nullptr, 0);
 }
Exemple #7
0
/****************************************************************
*
*	Function: main
*	Input:	int argc 	number of command line arguements
*		char **arg	pointer to those arguements
*
*	Output: int 0 for success and 1 for error
*
*	Description: Runs the simple merge algorithm multiple
*	times averaging the results and printing them to terminal. 
*
*****************************************************************/
int main(int argc, char **argv)
{
	struct timeval startt, endt, result;
	
	char name[8] = "omp/";

	int status=0;
	int n;
	int* S;
	int* R;
	
	int RUNS;
	
	//Check if app was given enough input
	if(argc < 6){
		printf("Missing Arguement Parameters\n");
		printf("Format ./seq path_input input_size ans_Path RUNS MAX_THREADS\n");
		return 1;
	}
	
	//Save args to memory and allocate memory for arrays
	n = atoi(argv[2])+1;
	RUNS = atoi(argv[4]);
	MAX_THREADS = atoi(argv[5]);
	S = malloc(n*sizeof(int));
	R = malloc(n*sizeof(int));

	if(n<50){
		chunk = 4; /*For Small N*/
	}

	omp_set_dynamic(0); //Makes sure the number of threads available is fixed    
	omp_set_num_threads(MAX_THREADS); //Set thread number


	if(S==NULL){
		printf("Failed to Allocate Memory for Input Array S");	
	}
	if(R==NULL){
		printf("Failed to Allocate Memory for Input Array R");	
	}

	//Read the input array from file and save to memory
	status = read_input(S, n, argv[1]);

	if(status){
		#ifdef DEBUG	
		printf("Failed to Read Input S\n");
		#endif
		return 1;
	}
	
	int *P_temp = malloc(n*sizeof(int));	
	int *R_temp = malloc(n*sizeof(int));
	int *P = malloc(n*sizeof(int));

	//Start of testing of the algorithm
	int j;
	double average;
	for(j=0; j<RUNS; j++){
		memset(R, 0, n*sizeof(int));

		/*Start Timer*/
		result.tv_sec=0;
		result.tv_usec=0;
		gettimeofday (&startt, NULL);

		/*Start Algorithm*/
		nodeLength(S, R, n, P_temp, R_temp, P);

		/*Stop Timer*/
		gettimeofday (&endt, NULL);
		result.tv_usec = (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
		average += result.tv_usec;
		
	}
	average = average/RUNS; //Average the execution times

	//print results to terminal
	printf("%d 	%f	us \n",n-1,average);

	if(atoi(argv[3])!=1)
	{
		status = outputCheck(R, argv[3], n);
		if(status){
			printf("Incorrect Answer\n");
		}
		else{
			printf("Correct Answer\n");
		}
	}
	

	/*Save the Results if the output is less than 50 elements*/
	if(n<=50){
		status = write_output(S, R, n, name);
	}

	if(status){	
		printf("Failed to Write Output \n");
		return 1;
	}

	free(S);
	free(R);
	free(P_temp);
	free(R_temp);
	free(P);	
	
    	return 0;
}
Exemple #8
0
/****************************************************************
*
*	Function: main
*	Input:	int argc 	number of command line arguements
*		char **arg	pointer to those arguements
*
*	Output: int 0 for success and 1 for error
*
*	Description: Runs the simple merge algorithm multiple
*	times averaging the results and printing them to terminal. 
*
*****************************************************************/
int main(int argc, char **argv)
{
	struct timeval startt, endt, result;
	
	char name[8] = "seq/";

	int status;
	int n;
	int* S;
	int* R;
	
	int RUNS;
	
	//Check if app was given enough input
	if(argc < 5){
		printf("Missing Arguement Parameters\n");
		printf("Format ./seq path_input input_size ans_Path RUNS\n");
		return 1;
	}
	
	//Save args to memory and allocate memory for arrays
	n = atoi(argv[2])+1;
	RUNS = atoi(argv[4]);
	S = malloc(n*sizeof(int));
	R = malloc(n*sizeof(int));


	if(S==NULL){
		printf("Failed to Allocate Memory for Input Array S");	
	}
	if(R==NULL){
		printf("Failed to Allocate Memory for Input Array R");	
	}

	//Read the input array from file and save to memory
	status = read_input(S, n, argv[1]);

	if(status){
		#ifdef DEBUG	
		printf("Failed to Read Input S\n");
		#endif
		return 1;
	}
	
	
	//Start of testing of the algorithm
	int j;
	double average;
	for(j=0; j<RUNS; j++){
		memset(R, 0, n*sizeof(int));

		/*Start Timer*/
		result.tv_sec=0;
		result.tv_usec=0;
		gettimeofday (&startt, NULL);

		nodeLength(S, R, n);

		/*Stop Timer*/
		gettimeofday (&endt, NULL);
		result.tv_usec = (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
		average += result.tv_usec;
		
	}
	average = average/RUNS; //Average the execution times

	//print results to terminal
	printf("%d 	%f	us \n",n-1,average);

	if(atoi(argv[3])!=1)
	{
		status = outputCheck(R, argv[3], n);
		if(status){
			printf("Incorrect Answer\n");
		}
		else{
			printf("Correct Answer\n");
		}
	}
	

	/*Save the Results if the output is less than 50 elements*/
	if(n<=50){
		status = write_output(S, R, n, name);
	}

	if(status){	
		printf("Failed to Write Output \n");
		return 1;
	}

	/*Used to generate the input files for the batch run*/
//	generateArrays();

	free(S);
	free(R);	
	
    	return 0;
}