Fix various multithreading related issues

This commit is contained in:
augustin64 2023-01-14 15:02:57 +01:00
parent dd6fb046c7
commit df48b92cf2

View File

@ -225,20 +225,20 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di
nb_remaining_images -= BATCHES / nb_threads; nb_remaining_images -= BATCHES / nb_threads;
} }
train_parameters[k]->start = BATCHES*j + (BATCHES/nb_threads)*k; train_parameters[k]->start = BATCHES*j + (BATCHES/nb_threads)*k;
train_parameters[k]->network = copy_network(network);
if (train_parameters[k]->start+train_parameters[k]->nb_images >= nb_images_total) { if (train_parameters[k]->start+train_parameters[k]->nb_images >= nb_images_total) {
train_parameters[k]->nb_images = nb_images_total - train_parameters[k]->start -1; train_parameters[k]->nb_images = nb_images_total - train_parameters[k]->start -1;
} }
if (train_parameters[k]->nb_images > 0) { if (train_parameters[k]->nb_images > 0) {
train_parameters[k]->network = copy_network(network);
pthread_create( &tid[k], NULL, train_thread, (void*) train_parameters[k]); pthread_create( &tid[k], NULL, train_thread, (void*) train_parameters[k]);
} else { } else {
tid[k] = 0; train_parameters[k]->network = NULL;
} }
} }
for (int k=0; k < nb_threads; k++) { for (int k=0; k < nb_threads; k++) {
// On attend la terminaison de chaque thread un à un // On attend la terminaison de chaque thread un à un
if (tid[k] != 0) { if (train_parameters[k]->network) {
pthread_join( tid[k], NULL ); pthread_join( tid[k], NULL );
accuracy += train_parameters[k]->accuracy / (float) nb_images_total; accuracy += train_parameters[k]->accuracy / (float) nb_images_total;
} }
@ -246,10 +246,12 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di
// On attend que tous les fils aient fini avant d'appliquer des modifications au réseau principal // On attend que tous les fils aient fini avant d'appliquer des modifications au réseau principal
for (int k=0; k < nb_threads; k++) { for (int k=0; k < nb_threads; k++) {
if (train_parameters[k]->network) { // Si le fil a été utilisé
update_weights(network, train_parameters[k]->network, train_parameters[k]->nb_images); update_weights(network, train_parameters[k]->network, train_parameters[k]->nb_images);
update_bias(network, train_parameters[k]->network, train_parameters[k]->nb_images); update_bias(network, train_parameters[k]->network, train_parameters[k]->nb_images);
free_network(train_parameters[k]->network); free_network(train_parameters[k]->network);
} }
}
current_accuracy = accuracy * nb_images_total/((j+1)*BATCHES); current_accuracy = accuracy * nb_images_total/((j+1)*BATCHES);
printf("\rThreads [%d]\tÉpoque [%d/%d]\tImage [%d/%d]\tAccuracy: "YELLOW"%0.2f%%"RESET" ", nb_threads, i, epochs, BATCHES*(j+1), nb_images_total, current_accuracy*100); printf("\rThreads [%d]\tÉpoque [%d/%d]\tImage [%d/%d]\tAccuracy: "YELLOW"%0.2f%%"RESET" ", nb_threads, i, epochs, BATCHES*(j+1), nb_images_total, current_accuracy*100);
fflush(stdout); fflush(stdout);