static int load_from_wait(queue *q_wait, queue *q_ready)
{
	if (empty_queue(q_wait)) {	/* cannot load from an empty queue */
		return FALSE;
	}

	/* attempt to load a process if it has a valid request vector and does not cause deadlock */
	process *check_proc = (process *) peek(q_wait);
	if (!(is_valid_request(check_proc) && is_no_deadlock(check_proc))) {
		return FALSE;
	}

	process *proc = (process *) dequeue(q_wait);

	/* necessary matrix and vector updates ORDER IS IMPORTANT */
	increment_row_vector(proc->max_need_vector, claim_matrix[proc->row_index]);
	increment_row_vector(proc->alloc_vector, allocation_matrix[proc->row_index]);
	decrement_row_vector(proc->alloc_vector, available_vector);
	clear_row_vector(proc->alloc_vector);
	matrix_difference(claim_matrix, allocation_matrix, difference_matrix);

	enqueue(q_ready, proc);

	return TRUE;
}
static int allocate_resources(process *proc, queue *q_ready, queue *q_process, queue *q_wait)
{
	if (!vector_leq(proc->request_vector, SYSTEM_RES)) {
		print_state_info(proc);
		printf("Process requested more resources than the system has available.\n");
		if (!empty_queue(q_process)) {
			int process_index = proc->row_index;
			free_process(proc);

			process *new_proc = (process *) dequeue(q_process);
			new_proc->row_index = process_index;

			clear_row_vector(allocation_matrix[new_proc->row_index]);
			clear_row_vector(claim_matrix[new_proc->row_index]);

			increment_row_vector(new_proc->max_need_vector, claim_matrix[new_proc->row_index]);
			matrix_difference(claim_matrix, allocation_matrix, difference_matrix);
			enqueue(q_ready, new_proc);
		}
		free_process(proc);

		return FALSE;
	}
	if (!is_valid_request(proc)) {
		print_state_info(proc);
		move_to_wait(q_wait, proc);

		return -1;
	}
	if (is_no_deadlock(proc)) {
		/* the resulting state after allocation is safe therefore allocate the resources this process requires */
		increment_row_vector(proc->request_vector, allocation_matrix[proc->row_index]);
		decrement_row_vector(proc->request_vector, available_vector);
		matrix_difference(claim_matrix, allocation_matrix, difference_matrix);

		return 0;
	} else {
		/* allocation of resources could potentially lead to a deadlock therefore enqueue this process */
		printf("\n>>> DEADLOCK POTENTIALLY AVOIDED <<<\n");
		print_state_info(proc);
		move_to_wait(q_wait, proc);

		return -1;
	}
}
static void move_to_wait(queue *q_wait, process *proc)
{
	/*
	 * clear all records of this process from the system matrices however save its current allocation row vector
	 * in the corresponding alloc_vector member of the process for later loading in load_from_wait()
	 */
	clear_row_vector(claim_matrix[proc->row_index]);
	increment_row_vector(allocation_matrix[proc->row_index], proc->alloc_vector);
	increment_row_vector(allocation_matrix[proc->row_index], available_vector);
	clear_row_vector(allocation_matrix[proc->row_index]);
	matrix_difference(claim_matrix, allocation_matrix, difference_matrix);

	enqueue(q_wait, proc);

	return;
}
Пример #4
0
int main(int argc, char * argv[])
{
    char cmdStr[MAX_PATH_LEN] = {0};
    char allready = 1;
    clock_t start_time, end_time;

    FILE_INFO_NODE * matrix_source[26] = { NULL };
    FILE_INFO_NODE * matrix_refer[26]  = { NULL };

    char refer_file_list[MAX_PATH_LEN]  = "refer_file_list";
    char source_file_list[MAX_PATH_LEN] = "source_file_list";

#if DEBUG_TRACE == 2
         {
             unsigned short argNums = argc;
             fprintf(stdout, "argc=%d\n",argc);
             while (argNums != 0) {
                 fprintf(stdout, "argv[%d]=%s\n",argNums,argv[argNums]);
                 argNums--;
             }
         } 
#endif

    ////////////////////////////////////////////////////////////////////////
    switch(argc)
    {
       case 2:   /* e.g. $lsd --help */ 
         if (argv[1][0] == '-' && 
             argv[1][1] == '-' &&
             argv[1][2] == 'h' &&
             argv[1][3] == 'e' &&
             argv[1][4] == 'l' &&
             argv[1][5] == 'p') 
         {
             fprintf(stdout, "Usage: lsd [SOURCE_DIR] [SUFFIX] [REFERENCE_DIR] [SUFFIX]\n");
             fprintf(stdout, "  or:  lsd -c SOURCE_FILE REFERENCE_FILE\n");
             fprintf(stdout, "  or:  lsd -a SOURCE_DIR REFERENCE_DIR\n");
             fprintf(stdout, "Print file path of the file that in SOURCE DIR but not in REFERENCE DIR with same PREFIX and difference SUFFIX\n");
             fprintf(stdout, "\n");
             fprintf(stdout, "    -c  comparinf specify SOURCE and REFERENCE file instead of DIR\n");
             fprintf(stdout, "    -a  comparing all types of file in specify SOURCE and REFERENCE directory\n");
             fprintf(stdout, "\n");
             fprintf(stdout, "For example:\n");
             fprintf(stdout, "   List file's path that in /home/src/cfiles/ but not in /home/src/objfiles/ with same PREFIX but difference SUFFIX.\n");
             fprintf(stdout, "   $lsd /home/src/cfiles/ .c /home/src/objfiles/ .obj\n");
             fprintf(stdout, "\n");
             fprintf(stdout, "   If already has SOURCE or REFERENCE file generated by,for example\n");
             fprintf(stdout, "       '$ls -name '*.c' > source_file_list'\n");
             fprintf(stdout, "   and '$ls -name '*.obj' > refer_file_list'\n");
             fprintf(stdout, "   then using '-c' option directly,as below:\n");
             fprintf(stdout, "   $lsd -c source_file_list refer_list_file\n");
         } else {
             goto MISSING_FILE_OPERAND;
         }
         allready = 0;
         break;
       case 4:   /* e.g.   $lsd -c source_file_list target_file_list */
                 /*     or $lsd -a source_path target_path           */
         if (argv[1][0] == '-' && argv[1][1] =='c') {
             fprintf(stdout,"Running...\n");
             strncpy(source_file_list,argv[2],MAX_PATH_LEN);
             strncpy(refer_file_list,argv[3],MAX_PATH_LEN);
         } else if (argv[1][0] == '-' && argv[1][1] =='a') {
             fprintf(stdout,"Running...\n");
             fprintf(stdout,"Generating source file list...\n");

             /* find all files but ignore hidden file and directory */
             sprintf(cmdStr,"find %s \\( ! -regex '.*/\\..*' \\) -xtype f -name '*' > source_file_list", argv[2]);
             printf("Generating source_file_list:\n");
             printf("   %s",cmdStr);
             if (system(cmdStr) != 0) {
                 fprintf(stderr, "Can not get source file list, please check your shell or path\n");
                 allready = 0;
                 break;
             } else {
                 fprintf(stdout,"Generating c file list...DONE\n");
             }

             fprintf(stdout,"Generating reference file list...\n");
             sprintf(cmdStr,"find %s \\( ! -regex '.*/\\..*' \\) -xtype f -name '*' > refer_file_list", argv[3]);
             printf("Generating refer_file_list:\n");
             printf("   %s",cmdStr);
             if (system(cmdStr) != 0) {
                 fprintf(stderr, "Can not get reference file list, please check your shell or path\n");
                 allready = 0;
                 break;
             } else {
                 fprintf(stdout,"Generating reference file list...DONE\n");
             }

         } else {
            goto MISSING_FILE_OPERAND;
         }
         break;
       case 5:   /* e.g. $lsd source_path .obj target_path .c */
         fprintf(stdout,"Running...\n");
         fprintf(stdout,"Generating source file list...\n");
         sprintf(cmdStr,"find %s \\( ! -regex '.*/\\..*' \\) -xtype f -name '*%s' > source_file_list", argv[1], argv[2]);
         if (system(cmdStr) != 0) {
             fprintf(stderr, "Can not get source file list, please check your shell or path\n");
             allready = 0;
             break;
         } else {
             fprintf(stdout,"Generating c file list...DONE\n");
         }

         fprintf(stdout,"Generating reference file list...\n");
         sprintf(cmdStr,"find %s \\( ! -regex '.*/\\..*' \\) -xtype f -name '*%s' > refer_file_list", argv[3], argv[4]);
         if (system(cmdStr) != 0) {
             fprintf(stderr, "Can not get reference file list, please check your shell or path\n");
             allready = 0;
             break;
         } else {
             fprintf(stdout,"Generating reference file list...DONE\n");
         }
         break;
       default:
MISSING_FILE_OPERAND:
         fprintf(stdout, "lsd: missing file operand\n");
         fprintf(stdout, "Try `lsd --help' for more information.\n");
         allready = 0;
         break;
    }

    ////////////////////////////////////////////////////////////////////////

    if (allready) {

        start_time = clock();

        matrix_get(refer_file_list,matrix_refer);
        matrix_get(source_file_list,matrix_source);

#if DEBUG_TRACE == 1 || DEBUG_TRACE == 2
        matrix_dump(matrix_source);
#endif

        matrix_difference(matrix_refer,matrix_source);
        matrix_dump(matrix_source);

        end_time = clock();
        fprintf(stdout,"Time consumation is %.4f seconds\n",(end_time - start_time)/(double)CLOCKS_PER_SEC);
    }

    return 0;
}
int main(int argc, char *argv[])
{
  start_high_priority();
  
#ifndef FIXED_COMPOUNDERS
  cout << "FIXED_COMPOUNDERS is unset. Please, set FIXED_COMPOUNDERS and recompile GBLAS" << endl;
  exit(0);
#endif
  
  cout << "Numerical Representation Noise Test (SGEMM)" << endl << endl;
  
  //srand ( (float)time(NULL) );
  srand ( 0 );
  
  const int NUM_ITER = 2;
  
  float alpha = 1.0f;
  float beta  = 0.0f;
  
  int NB = GBLAS_KERNEL_SIZE;
  int N = NB;
  int M = NB;
  int K = NB;
  
  float *A = (float*)_mm_malloc(sizeof(float)*M*K, 16);
  float *B = (float*)_mm_malloc(sizeof(float)*K*N, 16);
  float *C = (float*)_mm_malloc(sizeof(float)*M*N, 16);
  float *C_final = (float*)_mm_malloc(sizeof(float)*M*N, 16);
  float *C_diff = (float*)_mm_malloc(sizeof(float)*M*N*NUM_ITER, 16); 
  
  double mean, var;
  float MAX_VALUE_A, MAX_VALUE_B;
  
  MAX_VALUE_A = 4.0f;
  
  FILE* fout = fopen("20110905_num_repr_test_sgemm.dat", "wb"); // open write-binary

  for (double Aquant = (floor(5000/GBLAS_KERNEL_SIZE))*GBLAS_KERNEL_SIZE; Aquant <= 500000; Aquant += GBLAS_KERNEL_SIZE)
  {   
    MAX_VALUE_B = (float)floor((Aquant/(2*MAX_VALUE_A*GBLAS_KERNEL_SIZE)) + 0.5);
    
    cout << M << ", " << N << ", " << K << ", ";
    cout << MAX_VALUE_A << ", " << MAX_VALUE_B << ", " << Aquant << ", " << 2*K*MAX_VALUE_A*MAX_VALUE_B;
    for ( int e = 0; e < NUM_ITER; e++ )
    {
      set_matrix_2_value(C, M, N, 0.0f);
      set_matrix_2_value(C_final, M, N, 0.0f);
      
      set_matrix_random_integers_values(A, M, K, MAX_VALUE_A);
      set_matrix_random_integers_values(B, K, N, MAX_VALUE_B);
      
      cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K, alpha, A, K, B, N, beta, C_final, N);
      
      gblas_sgemm_mu(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K, alpha, A, K, B, N, beta, C, N, 100.0);
      
      matrix_difference(C_final, C, M, N, &C_diff[M*N*e]);
    }
    
    calculate_var_and_mean(C_diff, M*N*NUM_ITER, 1, mean, var);
    cout << ", " <<  mean << ", " << var;
    //cout << ", "; print_matrix_csv_notation(C_diff, 1, M*N*NUM_ITER);
    cout << endl;

    fwrite(C_diff, sizeof(float), M*N*NUM_ITER, fout);
  }

  fclose(fout);
  
  _mm_free(A);
  _mm_free(B);
  _mm_free(C);
  _mm_free(C_final);
  _mm_free(C_diff);
  
  exit_high_priority();
}
static int deallocate_resources(process *proc, queue *q_ready, queue *q_process, queue *q_wait, int is_end)
{
	if (is_end) {
		printf("\nSUCCESSFULLY TERMINATED PROCESS WITH PID %u\n", proc->pid);
		increment_row_vector(allocation_matrix[proc->row_index], available_vector);
		clear_row_vector(allocation_matrix[proc->row_index]);
		clear_row_vector(claim_matrix[proc->row_index]);

		if (load_from_wait(q_wait, q_ready)) {	/* attempt to load a process from the wait state */
			free_process(proc);
			return -1;
		}

		if (empty_queue(q_process)) {		/* no processes in the wait state so attempt to load from process queue */
			free_process(proc);
			return -1;
		}
		process *new_proc = (process *) dequeue(q_process);
		new_proc->row_index = proc->row_index;

		increment_row_vector(new_proc->max_need_vector, claim_matrix[new_proc->row_index]);
		matrix_difference(claim_matrix, allocation_matrix, difference_matrix);
		enqueue(q_ready, new_proc);
		free_process(proc);

		return -1;
	} else if (!vector_leq(proc->release_vector, allocation_matrix[proc->row_index])) {
		increment_row_vector(allocation_matrix[proc->row_index], available_vector);

		printf("\nProcess with PID %u ", proc->pid);
		print_row_vector(proc->release_vector, "attempted to release");
		print_row_vector(allocation_matrix[proc->row_index], "but only has allocated");

		clear_row_vector(allocation_matrix[proc->row_index]);
		clear_row_vector(claim_matrix[proc->row_index]);

		printf("\nABNORMAL TERMINATION OF PROCESS WITH PID %u\n", proc->pid);
		if (load_from_wait(q_wait, q_ready)) {
			free_process(proc);
			return -1;
		} else {
			if (empty_queue(q_process)) {
				free_process(proc);
				return -1;
			}
			process *new_proc = (process *) dequeue(q_process);
			new_proc->row_index = proc->row_index;

			increment_row_vector(new_proc->max_need_vector, claim_matrix[new_proc->row_index]);
			matrix_difference(claim_matrix, allocation_matrix, difference_matrix);
			enqueue(q_ready, new_proc);

			free_process(proc);

			return -1;
		}
	} else {		/* case were we can deallocate resources and have not reached an "END" instruction */
		decrement_row_vector(proc->release_vector, allocation_matrix[proc->row_index]);
		increment_row_vector(proc->release_vector, available_vector);
		matrix_difference(claim_matrix, allocation_matrix, difference_matrix);

		load_from_wait(q_wait, q_ready);	/* try to load one of the suspended processes */
		return 0;
	}
}