本文主要是介绍darknet源码剖析(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
pthread_t load_thread = load_data(args);
load_data位于src/data.c中
pthread_t load_data(load_args args)
{pthread_t thread;struct load_args *ptr = calloc(1, sizeof(struct load_args));*ptr = args;if(pthread_create(&thread, 0, load_threads, ptr)) error("Thread creation failed");return thread;
}
load_threads同样位于src/data.c中。
void *load_threads(void *ptr)
{int i;load_args args = *(load_args *)ptr;if (args.threads == 0) args.threads = 1;data *out = args.d;int total = args.n;free(ptr);data *buffers = calloc(args.threads, sizeof(data));pthread_t *threads = calloc(args.threads, sizeof(pthread_t));for(i = 0; i < args.threads; ++i){args.d = buffers + i;args.n = (i+1) * total/args.threads - i * total/args.threads;threads[i] = load_data_in_thread(args);}for(i = 0; i < args.threads; ++i){pthread_join(threads[i], 0);}*out = concat_datas(buffers, args.threads);out->shallow = 0;for(i = 0; i < args.threads; ++i){buffers[i].shallow = 1;free_data(buffers[i]);}free(buffers);free(threads);return 0;
}
在load_threads函数中,args.threads=64,因此共调用load_data_in_thread 64次,结合pthread_join,可以知道load_data_in_thread创建了64个线程。
data *buffers = calloc(args.threads, sizeof(data));共为64个data数据结构分配内存。
for(i = 0; i < args.threads; ++i){args.d = buffers + i;args.n = (i+1) * total/args.threads - i * total/args.threads;threads[i] = load_data_in_thread(args);}
args.d分别指向64个预先分配的内存空间。total为net->batch * net->subdivisions * ngpus,在本程序中为64*16*1=1024,arg.n为16,进入load_data_in_thread函数。
pthread_t load_data_in_thread(load_args args)
{pthread_t thread;struct load_args *ptr = calloc(1, sizeof(struct load_args));*ptr = args;if(pthread_create(&thread, 0, load_thread, ptr)) error("Thread creation failed");return thread;
}
每个线程再次启动线程。进入load_thread函数。
else if (a.type == DETECTION_DATA){*a.d = load_data_detection(a.n, a.paths, a.m, a.w, a.h, a.num_boxes, a.classes, a.jitter, a.hue, a.saturation, a.exposure);
type是在train_detector中设置的,进入load_data_detection函数。
char **random_paths = get_random_paths(paths, n, m);
进入get_random_paths函数。
char **get_random_paths(char **paths, int n, int m)
{char **random_paths = calloc(n, sizeof(char*));int i;pthread_mutex_lock(&mutex);for(i = 0; i < n; ++i){int index = rand()%m;random_paths[i] = paths[index];//if(i == 0) printf("%s\n", paths[index]);}pthread_mutex_unlock(&mutex);return random_paths;
}
其中paths为所有训练数据集保存地址,n为16,m为训练数据集长度。通过上述程序可以得到随机的n个训练数据集地址。
共有64个线程,每个线程16个地址,因此共1024个训练数据集地址。
这篇关于darknet源码剖析(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!