void init_matrices() {
	for (unsigned i=0; i<nmats.get_value(); i++) {
		matricesA.push_back( new_matrix(0, matsz.get_value()) );
		matricesB.push_back( new_matrix(0, matsz.get_value()) );
		matricesC.push_back( new_matrix(0, matsz.get_value()) );
	}
}
void free_matrices() {
	for (unsigned i=0; i<nmats.get_value(); i++) {
		free(matricesA[i]);
		free(matricesB[i]);
		free(matricesC[i]);
	}
}
Пример #3
0
int validate_arguments()
{
    if (!start_i.was_set()) {
        cerr << "Error: you must provide the start file index."
        << "(use -h for help)" << endl;
        return 1;
    }

    if (!end_i.was_set()) {
        cerr << "Error: you must provide the end file index."
        << "(use -h for help)" << endl;
        return 1;
    }

    if (!basename_main.was_set()) {
        cerr << "Error: you must provide the basename."
        << "(use -h for help)" << endl;
        return 1;
    }

    if (end_i.get_value() < start_i.get_value()) {
        cerr << "Error: start index must be less (<) or equal (=) to end index"
        << "(use -h for help)" << endl;
        return 1;
    }

    if (lt.was_set()) {
        if (wt.was_set()) {
            cerr << "Error: both -lt and -lw were set, select only one." << endl;
            return 1;
        }
        else {
            rain::RF_Technique::set_system_threshold(LINUX_SYS_THRESHOLD);
        }
    }
    else {
        if (wt.was_set()) {
            rain::RF_Technique::set_system_threshold(WINDOWS_SYS_THRESHOLD);
        }
        else {
            cerr << "Error: either -lt or -lw must be selected." << endl;
            return 1;
        }
    }

    return 0;
}
int main(int argc, char *argv[]) {
#ifdef __MIGRATE__
	hwloc_topology_init(&topology);
	hwloc_topology_load(topology);
#endif

	if (clarg::parse_arguments(argc, argv)) {
		cerr << "Error when parsing the arguments!" << endl;
		return 1;
	}

	if (pthread_mutex_init(&lock, NULL) != 0) {
		printf("\n mutex init failed\n");
		return 1;
	}

	if (nmats.get_value() < 1) {
		cerr << "Error, nm must be >= 1" << endl;
		return 1;
	}

	init_matrices();

	int n_threads = nthreads.get_value();
	std::vector<pthread_t> threads(n_threads);

    double tempo = mysecond();
    
	for(int t=0; t<n_threads; t++)
		pthread_create(&threads[t], NULL, worker_thread, (void*)t);

	for(int t=0; t<n_threads; t++)
		pthread_join(threads[t], NULL);

    tempo = mysecond() - tempo;
	//printf("%d %f \n", matsz.get_value(), tempo);
	
	free_matrices();
	
#ifdef __MIGRATE__  
	hwloc_topology_destroy(topology);
#endif 
	
	return 0;
}
int get_matrix()
{
	int id; 

	if (next_matrix >= nmats.get_value())
		return -1;

	pthread_mutex_lock(&lock);
		id = next_matrix++;
	pthread_mutex_unlock(&lock);

	return id;
}
void* worker_thread(void* m) {
	int matrix_id;
	
	while ( (matrix_id = get_matrix()) >= 0 ) {
		double* A = matricesA[matrix_id];
		double* B = matricesB[matrix_id];
		double* C = matricesC[matrix_id];
		unsigned N = matsz.get_value();

		double tmp;
		unsigned i,j,k;

#ifdef __MIGRATE__

		hwloc_bitmap_t set = hwloc_bitmap_alloc();

		hwloc_get_cpubind(topology, set, HWLOC_CPUBIND_THREAD);
		hwloc_get_last_cpu_location(topology, set, HWLOC_CPUBIND_THREAD);

		hwloc_bitmap_singlify(set);

		hwloc_set_area_membind ( topology, (const void*)A, N*N*sizeof(double), (hwloc_const_cpuset_t)set, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_MIGRATE );		
		hwloc_set_area_membind ( topology, (const void*)B, N*N*sizeof(double), (hwloc_const_cpuset_t)set, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_MIGRATE );		
		hwloc_set_area_membind ( topology, (const void*)C, N*N*sizeof(double), (hwloc_const_cpuset_t)set, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_MIGRATE );		

		hwloc_bitmap_free(set);
#endif

		for (i=0; i<N; i++) {
			for (j=0; j<N; j++) {
				tmp=0;
				for (k=0; k<N; k++)
					tmp += A[i*N + k] * B[k*N + j];

				C[i*N + j] = tmp;
			}
		}

	} //while ( (matrix_id = get_matrix()) >= 0 )

	return NULL;
}
Пример #7
0
int main(int argc,char** argv)
{
    // Parse the arguments
    if (clarg::parse_arguments(argc, argv)) {
        cerr << "Error when parsing the arguments!" << endl;
        return 1;
    }

    if (help.get_value() == true) {
        usage(argv[0]);
        return 1;
    }

    if (validate_arguments())
        return 1;

    // Create the input pipe.
    trace_io::raw_input_pipe_t in(basename_main.get_value(),
                                  start_i.get_value(),
                                  end_i.get_value());

    // Current and next instructions.
    trace_io::trace_item_t current;
    trace_io::trace_item_t next;

    // Fetch the next instruction from the trace
    if (!in.get_next_instruction(current)) {
        cerr << "Error: input trace has no instruction items." << endl;
        return 1;
    }

    rain::RF_Technique* rf = new rf_technique::NET();

    // While there are instructions

    long long int insn_count=0;
    long long int jmps=0;
    long long int jmps_taken=0;
    long long int invalid_insn=0;
    long long int cc_gen=0;
    long long int cc_used=0;
    
    bool was_jump = false;
	
	char addr_decoder[16];
	char addr_trace[16];

    memset(addr_decoder,0,16);
    memset(addr_trace,0,16);
	
    ud_t ud_obj;

    ud_init(&ud_obj);
    ud_set_syntax(&ud_obj, UD_SYN_INTEL); //Intel syntax
    ud_set_mode(&ud_obj, 32); //32-bit
	
    while ( in.get_next_instruction(next) ) {
                     
        ++insn_count;
        
        // Process the trace
        //if (rf) { rf->process(current.addr, current.opcode, current.length, next.addr, next.opcode, next.length); }

        ud_set_input_buffer(&ud_obj,(uint8_t*)current.opcode,(size_t)current.length);
        ud_set_pc(&ud_obj, (uint64_t) current.addr); //set IP/EIP register

        ud_disassemble(&ud_obj);

//LCCE - Lazy Code Condition Evaluation 'prototype' analysis
	
		if (  ModifiesFlags( ud_insn_mnemonic(&ud_obj) ) == true  ) {++cc_gen;}
		if (  TestsFlags( ud_insn_mnemonic(&ud_obj) ) == true  ) {++cc_used;}

		
//Indirect Jump 'pre-prototype' analysis       
        if (was_jump == true)  {
			
			sprintf(addr_trace,"%llx",current.addr);
			
			if ( strcmp(addr_trace,addr_decoder) == 0 ) { ++jmps_taken; }
			
			was_jump = false;
			memset(addr_decoder,0,16);
			memset(addr_trace,0,16);
		}
               
        switch ( ud_insn_mnemonic(&ud_obj) ) { //haow about use a lookup table? It worths?
/*			case UD_Ijmp: //must count jmp ?
				++jmps;
				break;
*/
			case UD_Ijo:
			case UD_Ijno:
			case UD_Ijb:
			case UD_Ijae:
			case UD_Ijz:
			case UD_Ijnz:
			case UD_Ijbe:
			case UD_Ija:
			case UD_Ijs:
			case UD_Ijns:
			case UD_Ijp:
			case UD_Ijnp:
			case UD_Ijl:
			case UD_Ijge:
			case UD_Ijle:
			case UD_Ijg:
			case UD_Ijcxz:
			case UD_Ijecxz:
			case UD_Ijrcxz:
				++jmps;
				was_jump = true;
				ExtractAddr(ud_obj.asm_buf, addr_decoder, sizeof(addr_decoder), 10); //10 can change...it's 'ad-hoc'...
				break;
			
			case UD_Iinvalid:
				++invalid_insn;
			
			default:
				break;
		}
//			

        //printf( "0x%llx: %s\n", current.addr, ud_insn_asm(&ud_obj) ); //prints instruction

        current = next;
        
    }
	
	//printf is just for now - we need to use statistical class and dump on csv, not STDOUT
	printf("\nTotal de inst.\t\t\t= %lld\n", insn_count);
	printf("Inst. mal-decodificadas\t\t= %lld\n", invalid_insn);
	printf("Total de jumps cond.\t\t= %lld\n", jmps);
    printf("Jumps cond. tomados\t\t= %lld\n", jmps_taken);
    printf("Inst. que geram cond. codes\t= %lld\n", cc_gen);
    printf("Inst. que usam cond. codes\t= %lld\n", cc_used);
    
    
    if (rf) rf->finish();

    //Print statistics

    if (rf) {

        ofstream reg_stats_f(reg_stats_fname.get_value().c_str());
        rf->rain.printRAInStats(reg_stats_f);
        reg_stats_f.close();

        ofstream overall_stats_f(overall_stats_fname.get_value().c_str());
        rf->rain.printOverallStats(overall_stats_f);
        overall_stats_f.close();

        // dumping dot regions graph
        if (dotgraph.was_set()) {
            std::string file = dotgraph.get_value();
            rf->rain.printRegionsDOT(file);

            //rf->rain.
        }
    }

    return 0; // Return OK.
}