I have a genetic code and I want parallel it on 12 cores, first part is a loop that I want to parallel it, but after parallel and run, I see serial code is faster than parallel code, please tell me where i am wrong. Is there a way that the openmp faster than serrial?
int main() {
srand(time(0));
srand(rand() % 10000007);
G.vertex_degrees();
int step = 0, n_pop = 100;
G.initial_population(n_pop);//parallel shod
G.full_random_array();//movazi shod
clock_t start = clock();
int i = 0,tid,nthreads;
#pragma omp parallel for
for (i = 0; i < n_pop; i++) {
G.Chromosome_Repairing_Method(i);
G.Fitness(i);
}
//cout << "MC Size :" << G.current_Bc_size << " with Time (precise) = " << ((double)(clock() - start)) / CLOCKS_PER_SEC << "found it" << endl;
omp_set_num_threads(4);
G is an object of genetic algorithm and G.Chromosome_Repairing_Method(i); is a function that repair chromosome and G.Fitness(i); is a function that calculate fitness of each chromosome