On Fri, Oct 09, 2015 at 13:58:32 +0200, Bernd Schmidt wrote: > One oddity I noticed in target.c is that there are two different num_devices > variables: > > /* Total number of available devices. */ > static int num_devices; > > /* Number of GOMP_OFFLOAD_CAP_OPENMP_400 devices. */ > static int num_devices_openmp; > > Confusingly, the get_num_devices function returns num_devices_openmp. That > function includes a pthread_once call to gomp_target_init, which sets up > these variables. References to num_devices_openmp through get_num_devices > are thereforce guaranteed to be initialized. However, there are direct > references to num_devices, in GOMP_offload_register_ver and > GOMP_offload_unregister_ver, and they don't seem to enforce any kind of > initialization: > > /* Load image to all initialized devices. */ > for (i = 0; i < num_devices; i++) > { > struct gomp_device_descr *devicep = &devices[i]; > gomp_mutex_lock (&devicep->lock); > if (devicep->type == target_type && devicep->is_initialized) > gomp_load_image_to_device (devicep, version, > host_table, target_data, true); > gomp_mutex_unlock (&devicep->lock); > } > > I'm guessing this only triggers when dlopening something with an offload > image after devices have been initialized already, and it looks like we have > symmetrical code in gomp_init_device.
Right, this code offloads given image to all initialized devices, and similar code in gomp_init_device offloads all registered images to a given device. > Wouldn't it be possible/better to > force a gomp_target_init before referencing num_devices, and then relying on > the code I quoted and deleting the image loading from gomp_init_device? gomp_target_init only loads plugins and sets num_devices/num_devices_openmp, but it doesn't call gomp_init_device, because we wanted to defer device initialization as much as possible. So, gomp_init_device is called immediately before usage of that device. -- Ilya