Ejemplo n.º 1
0
grid hard_sudoku(){
    grid board = fill_with_zeros();
    board.array[0][0] = 8;
    board.array[0][3] = 4;
    board.array[0][5] = 6;
    board.array[0][8] = 7;
    board.array[1][6] = 4;
    board.array[2][1] = 1;
    board.array[2][6] = 6;
    board.array[2][7] = 5;
    board.array[3][0] = 5;
    board.array[3][2] = 9;
    board.array[3][4] = 3;
    board.array[3][6] = 7;
    board.array[3][7] = 8;
    board.array[4][4] = 7;
    board.array[5][1] = 4;
    board.array[5][2] = 8;
    board.array[5][4] = 2;
    board.array[5][6] = 1;
    board.array[5][8] = 3;
    board.array[6][1] = 5;
    board.array[6][2] = 2;
    board.array[6][7] = 9;
    board.array[7][2] = 1;
    board.array[8][0] = 3;
    board.array[8][3] = 9;
    board.array[8][5] = 2;
    board.array[8][8] = 5;

    return board;
}
int main(){
	int i,j,len, carry;
	int tmp;
	char *pos;
	
	fill_with_zeros(output,MAX);
	while(1){
		fgets(number, MAX, stdin);	
		if ((pos=strchr(number, '\n')) != NULL)
    			*pos = '\0';
		len = strlen(number);
		if(len == 1 && number[0] == '0')
			break;
		
		fill_with_zeros(number, len);
		carry = 0;
		j = strlen(output)-1;
		for(i=strlen(number)-1; i >= 0; --i, --j){
			number[i] = ((number[i] - '0') + carry)+'0';			
			tmp = (number[i] - '0') + (output[j] - '0');
			if(tmp >= 10){
				carry = 1;
				tmp -= 10;
			}else{
				carry = 0;
			}
			output[j] = (tmp)+'0';
		}	
	}
	j=0;
	while(output[j++] == '0');
	for(i=j-1; i<strlen(output); i++)
		printf("%c", output[i]);
	printf("\n");
	return 0;
}
Ejemplo n.º 3
0
int main(int argc, char **argv){

	/* Variables declaration */
	int i;
	int j;
	
	/* Number of rounds to run the algorithm */
	int rounds = 0;
	
	/* Total size of the array */
	int total_size = (rows * columns) / participants;
	
	printf("Total amount of work is %d\n", total_size);
	
	/* the amount of work that is assigned to worker1 */
	int amount_of_work = floor( (worker1 + 1) * columns / participants ) - floor( worker1 * columns / participants );
	
	printf("amount of work is %d\n", amount_of_work);
	
	/* Session start */
	session *s;
	join_session(&argc, &argv, &s, "worker1.spr");
	role *master = s->get_role(s, "master");
	
	/* Dynamic memory allocation of array C */
	int *C = NULL;
	C = (int *) malloc(columns * sizeof(int));
	
	if(C == NULL)
	{
		/* Terminate program if there is not enough memory */ 
		fprintf(stderr, "out of memory \n");
		exit(0);
	}

	/* Declaration of the main array */
	int *Beta_worker1 = NULL;
	
	/* Dynamic memory allocation */
	Beta_worker1 = (int *) malloc( total_size * sizeof(int) );
	
	/* Abort if there is not enough memory */
	if(Beta_worker1 == NULL){
		fprintf(stderr, "out of memory\n");
		exit(-1);
	}	
	
	/* The array that will hold the results */
	int *worker1_results = NULL;
	worker1_results = (int *) malloc( amount_of_work * sizeof(int) );
	
	/* Fill with zeros the result array */
	fill_with_zeros(worker1_results, amount_of_work);
	
	/* The size of the arrays that master waits from the workers */
	size_t array_C = columns;
	size_t array_Beta = total_size;	
	
	/* worker1 receives the arrays in order to do the computation */
	recv_int_array(master, C, &array_C);
	recv_int_array(master, Beta_worker1, &array_Beta);
	
/* Run for 1000 rounds */	
while(rounds++ < 1000){	
	/* Main computation from master */
	for(i = 0; i < amount_of_work; i++)
		for(j = 0; j < columns; j++)
			worker1_results[i] += Beta_worker1[i * amount_of_work + j] * C[j];
}//End of while	
	
	/* worker1 sends the results to the master */
	send_int_array(master, worker1_results, amount_of_work);
	
	/* End Session */
	end_session(s);	
	
	/* Deallocate memmory */
	free(C);
	C = NULL;
	free(Beta_worker1);
	Beta_worker1 = NULL;
	free(worker1_results);
	worker1_results = NULL;
	
	return EXIT_SUCCESS;
}
Ejemplo n.º 4
0
void setup() {
	config_wireless();
	CC2500_Read(&status_byte, CC2500_STX, 1); // signal
	fill_with_zeros(buffer, 64);
}
Ejemplo n.º 5
0
int main(int argc ,char **argv){

	/* Declare start and end of computation time */
	struct timeval start, end;

	/* Variables declaration */
	int i;
	int j;
	int rows_of_proc;

	/* Number of rounds that the parallel part will be computed */
	int rounds = 0;
	
	/* Variable that shows the non zero elemets of the array */
	int non_zero_elements = 0;	

	/* Session start */
	//session *s;
	//join_session(&argc, &argv, &s, "Master.spr");
	//role *worker1 = s->get_role(s, "worker1");		
	//role *worker2 = s->get_role(s, "worker2");
	//role *worker3 = s->get_role(s, "worker3");
	
	/* Declaration of array results, which is the vector 
	that will hold the results */
	int *results = NULL;
	
	/* Declaration of array x, which is the vector 
	that will bw multiplied with the array */
	int *x = NULL;
	//Dynamic memory allocation
	x = (int *) malloc( ncolumns * sizeof(int) );
	if(x == NULL)
	{
		fprintf(stderr, "out of memory\n");
		exit(-1);
	}
	
	//Fill the vector x
	printf("Filling and printing vector x \n");
	fill_vector(x, ncolumns);
	
	
	/* The array that contains the start of each row within
	the non-zero elements array*/
	int *row_ptr = NULL;
	
	/* Dynamic memory allocation of the array row_ptr */
	row_ptr = (int *) malloc( (nrows + 1) * sizeof(int) );
	/* Check if we have enough memory */
	if(row_ptr == NULL)
	{
		fprintf(stderr, "out of memory\n");
		exit(-1);
	}	

	//Declaration of array A
	int **A = NULL;
		
	//Dynamic memory allocation of the matrix
	A = malloc(nrows * sizeof(int *));
	
	/* Check if we have enough memory */
	if(A == NULL)
	{
	fprintf(stderr, "out of memory\n");
	exit(-1);
	}
	for(i = 0; i < nrows; i++)
	{
		A[i] = malloc(ncolumns * sizeof(int));
		/* Check if we have enough memory */
		if(A[i] == NULL)
		{
			fprintf(stderr, "out of memory\n");
			exit(-1);
		}
	}
/*
	A[0][0] = 5;
	A[0][1] = 1;
	A[0][2] = 0;
	A[0][3] = 0;
	A[0][4] = 0;
	A[0][5] = 0;
	
	A[1][0] = 0;
	A[1][1] = 6;
	A[1][2] = 0;
	A[1][3] = 7;
	A[1][4] = 0;
	A[1][5] = 8;
	
	A[2][0] = 0;
	A[2][1] = 0;
	A[2][2] = 1;
	A[2][3] = 0;
	A[2][4] = 0;
	A[2][5] = 0;
	
	A[3][0] = 0;
	A[3][1] = 0;
	A[3][2] = 2;
	A[3][3] = 0;
	A[3][4] = 3;
	A[3][5] = 2;
	
	A[4][0] = 9;
	A[4][1] = 0;
	A[4][2] = 0;
	A[4][3] = 1;
	A[4][4] = 4;
	A[4][5] = 0;
	
	A[5][0] = 1;
	A[5][1] = 0;
	A[5][2] = 2;
	A[5][3] = 3;
	A[5][4] = 0;
	A[5][5] = 1;	
*/	
	
	/* Fill tha matrix with values */
	fill_matrix(A);

	/* Start counting the time */	
	gettimeofday(&start, NULL);
	
	/* Computation of non zero elements */
	non_zero_elements = num_of_non_zero_elements(A);	
	printf("Num of non zeros is %d \n", non_zero_elements);	
	
	/* Master sends the non zero elements to the workers */
	//send_int(worker1, non_zero_elements);
	//send_int(worker2, non_zero_elements);
	//send_int(worker3, non_zero_elements);
	
	/* Dynamically allocate memory for the array results */
	results = (int *) malloc( nrows * sizeof(int) );
	fill_with_zeros(results, nrows);	
	
	/* Declaration af array values and memory allocation */
	int *values = (int *) malloc(non_zero_elements * sizeof(int));
	
	/* Check if we have enough memory */
	if(values == NULL)
	{
		fprintf(stderr, "out of memory\n");
		exit(-1);
	}		

	/* Fill the values array */
	fill_values(A, values);	
	
	/* Declaration of the array values and dynamic memory allocation  */
	int *col_ind = (int *) malloc( non_zero_elements * sizeof(int) );
	
	/* Check if we have enough memory */
	if(col_ind == NULL)
	{
		fprintf(stderr, "out of memory\n");
		exit(-1);
	}	
	
	/* Fill the col_ind array */
	fill_col_ind(A, col_ind);
	
	/* Fill the row_ptr array with zero*/
	fill_with_zeros(row_ptr, nrows + 1);
	
	/* Print row_ptr initialized */
	////////////////print_vector(row_ptr, nrows + 1);
	
	/* Fill the row_ptr array */
	fill_row_ptr(A, row_ptr);	
	
	/* The results that workers will send to master */
	int *buffer_results[participants] = {NULL};
	
	/* Master sends tha arrays to workers */
	//send_int_array(worker1, row_ptr, nrows + 1);
	//send_int_array(worker1, values, non_zero_elements);
	//send_int_array(worker1, col_ind, non_zero_elements);
	//send_int_array(worker1, x, ncolumns);
	
	//send_int_array(worker2, row_ptr, nrows + 1);
	//send_int_array(worker2, values, non_zero_elements);
	//send_int_array(worker2, col_ind, non_zero_elements);
	//send_int_array(worker2, x, ncolumns);
	
	//send_int_array(worker3, row_ptr, nrows + 1);
	//send_int_array(worker3, values, non_zero_elements);
	//send_int_array(worker3, col_ind, non_zero_elements);
	//send_int_array(worker3, x, ncolumns);	
	
	/*
	print_vector(values, non_zero_elements);
	printf("\n");
	print_vector(row_ptr, nrows + 1);	
	printf("\n");
	print_vector(col_ind, non_zero_elements);
	*/

	//print_vector(values, non_zero_elements);
	
	/* Define the work that master will do */
	/*int start_work_i[participants];
	int end_work_i[participants];*/
	
	int start_work[participants];
	int end_work[participants];	
	
	int amount_of_work[participants];
	
	for(i = 0; i < participants; i++){
	
		/* The amount of work that the master will do */
		start_work[i] = floor((i * nrows)/participants);
		end_work[i] = floor(((i + 1) * nrows)/participants);	
		amount_of_work[i] = end_work[i] - start_work[i];
	
		printf("start = %d - end = %d - amount of work = %d\n", start_work[i], end_work[i], amount_of_work[i]);

		/* Dynamic allocation of buffer results */
		buffer_results[i] = (int *) malloc(amount_of_work[i] * sizeof(int));
		if(buffer_results[i] == NULL){
			fprintf(stderr, "Out of memory, aborting program...\n");
		}
	
	}
	
	/* The amount of work that the master will do */
	//start_work[master] = floor((master * nrows)/participants);
	//end_work[master] = floor(((master + 1) * nrows)/participants);	
	
	//printf("start = %d - end = %d\n", start_work[0], end_work[0]);

/* Run for 40 rounds */	
while(rounds++ < 40){	
	/* Main computation of the result. Each processor
	computes the work that is assigned to it*/
	for(i = start_work[master]; i < end_work[master]; i++){
		for(j = row_ptr[i]; j < row_ptr[i + 1]; j++)
			buffer_results[0][i] += values[j] * x[col_ind[j]];
			//results[i] += values[j] * x[col_ind[j]];
	}
}	

	/* Master node receives the results */
	//receive_int_array(worker1, buffer_results[1], amount_of_work[1]);
	//receive_int_array(worker2, buffer_results[2], amount_of_work[2]);
	//receive_int_array(worker3, buffer_results[3], amount_of_work[3]);
	
	/* End counting of time here */
	gettimeofday(&end, NULL);
	
	/* Computation of time */
	long time_elapsed = ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)) / 1000000;
	 
	/* Print the time that has elapsed */
	printf("Elapsed time is: %ld seconds\n", time_elapsed);	
	
/*
int counter = 0;	
while(counter++ < 1000){
	for(i = 0; i < nrows; i++){
		//printf("rank = %d - i = %d\n", rank, i);
		//printf("RANK = %d\n", rank);
		for(j = row_ptr[i]; j < row_ptr[i + 1]; j++)
			results[i] += values[j] * x[col_ind[j]];
			//results[i - start] += values[j] * x[col_ind[j]];
	}

}*/
	
	/* Define the sub-buffers that we will send to the participants */
	/*int *buffer_values[participants] = {NULL};
	int *buffer_col_ind[participants] = {NULL};
	int *buffer_row_ptr[participants] = {NULL};*/
	
	/* Compute which part of the array each participant will compute */
	//for(i = 0; i < participants; i++){
		/*start_work_i[i] = floor((i * nrows)/participants);
		end_work_i[i] = floor(((i + 1) * nrows)/participants);

		start_work[i] = floor((i * non_zero_elements)/participants);
		end_work[i] = floor(((i + 1) * non_zero_elements)/participants);		

		amount_of_work[i] = floor(((i + 1) * non_zero_elements)/participants) - floor(( i * non_zero_elements)/participants);
		//amount_of_work[i] = end_work[i] - start_work[i];
		printf("start = %d - end = %d - amount of work = %d\n", start_work[i], end_work[i], amount_of_work[i]);
		*/
		/* Dynamically buffer allocation */
		/*buffer_values[i] = (int *) malloc( (amount_of_work[i]) * sizeof(int));
		buffer_col_ind[i] = (int *) malloc( (amount_of_work[i]) * sizeof(int));
		*/
		//buffer_values[i] = (int *) malloc( (non_zero_elements) * sizeof(int));
		//buffer_col_ind[i] = (int *) malloc( (non_zero_elements) * sizeof(int));
		
		/* Check if memory is enough */
		/*if(buffer_values[i] == NULL){
			fprintf(stderr, "Out of memory.Aborting the program...\n");
			exit(0);
		}*/

		/* Check if memory is enough */
		/*if(buffer_col_ind[i] == NULL){
			fprintf(stderr, "Out of memory.Aborting the program...\n");
			exit(0);
		}*/	
	//}

	////////////////////////print_array(A);
	////////////////////////print_vector(row_ptr, nrows + 1);
	
	//printf("row_ptr[%d] = %d\n", start_work[0], row_ptr[start_work[0]]);
	//printf("row_ptr[%d] = %d\n", end_work[0], row_ptr[end_work[0]]);
	
	//print_vector(buffer_values[0], amount_of_work[0]);
	//printf("\n\n");
	
	
	
	/* Compute the buffer for each participant (amount of work) */
	/*for(i = 0; i < participants; i++){
		memcpy( buffer_values[i], values + start_work[i], amount_of_work[i] * sizeof(int) );
		memcpy( buffer_col_ind[i], col_ind + start_work[i], amount_of_work[i] * sizeof(int));
		//printf("buffer[%d] = %s\n", i, buffer[i]);
	}*/	
	
	//print_vector(buffer_values[0], amount_of_work[0]);
	//print_vector(buffer_col_ind[0], amount_of_work[0]);
	//print_vector(col_ind, non_zero_elements);
	//print_vector(values, non_zero_elements);
	
	////////print_vector(col_ind, non_zero_elements);
	
	/* Main computation of the result. Master
	computes the work that is assigned to it*/
	/*for(i = start_work[0]; i < end_work[0]; i++){
		for(j = row_ptr[i]; j < row_ptr[i + 1]; j++)
			results[i] += values[j] * x[col_ind[j]];
			//results[i - start] += values[j] * x[col_ind[j]];
	}*/
	/*
	for(i = start_work_i[1]; i < end_work_i[1]; i++){
		for(j = row_ptr[i]; j < row_ptr[i + 1]; j++){
			
			printf("j = %d\n", j);
			results[i] += buffer_values[1][j] * x[buffer_col_ind[1][j]];
		}	
	}	*/
		
	//printf("buffer_values[%d][%d] = %d\n", 1, 12, buffer_values[1][12]);	
		
	/* Main computation of the result. Each processor
	computes the work that is assigned to it*/
	/*for(i = start; i < end; i++){
		//printf("rank = %d - i = %d\n", rank, i);
		//printf("RANK = %d\n", rank);
		for(j = row_ptr[i]; j < row_ptr[i + 1]; j++)
			y[i] += values[j] * x[col_ind[j]];
			//results[i - start] += values[j] * x[col_ind[j]];
	}*/	
	
	
	/* The arrays that we will send to the workers */
	//int *buffer_col_ind = NULL;
	//int *buffer_values = NULL;

	/* Free memory */
	free(x);
	free(results);
	free(values);
	free(col_ind);
	free(row_ptr);
	
	return EXIT_SUCCESS;
}