def func(env): global counter with env: nthreads = os.getenv("OMP_NUM_THREADS") expected = omp_num_threads() with pool_threading() as pool: assert_equal(int(os.environ["OMP_NUM_THREADS"]), 1) if mkl is not None: assert_equal(mkl.get_max_threads(), 1) counter = 0 pool.map(func_thread, range(pool._processes)) assert_equal(os.getenv("OMP_NUM_THREADS"), nthreads) if mkl is not None: assert_equal(mkl.get_max_threads(), mkl_nthreads) assert_equal(counter, expected) assert_not_in("OMP_NUM_THREADS", os.environ)
def func(env): global counter with env: nthreads = os.getenv('OMP_NUM_THREADS') expected = omp_num_threads() with pool_threading() as pool: assert_equal(int(os.environ['OMP_NUM_THREADS']), 1) if mkl is not None: assert_equal(mkl.get_max_threads(), 1) counter = 0 pool.map(func_thread, range(pool._processes)) assert_equal(os.getenv('OMP_NUM_THREADS'), nthreads) if mkl is not None: assert_equal(mkl.get_max_threads(), mkl_nthreads) assert_equal(counter, expected) assert_not_in('OMP_NUM_THREADS', os.environ)
def _get_projection_operator(rotation, scene, nu, position, synthbeam, horn, primary_beam, verbose=True): ndetectors = position.shape[0] ntimes = rotation.data.shape[0] nside = scene.nside thetas, phis, vals = QubicInstrument._peak_angles( scene, nu, position, synthbeam, horn, primary_beam) ncolmax = thetas.shape[-1] thetaphi = _pack_vector(thetas, phis) # (ndetectors, ncolmax, 2) direction = Spherical2CartesianOperator('zenith,azimuth')(thetaphi) e_nf = direction[:, None, :, :] if nside > 8192: dtype_index = np.dtype(np.int64) else: dtype_index = np.dtype(np.int32) cls = { 'I': FSRMatrix, 'QU': FSRRotation2dMatrix, 'IQU': FSRRotation3dMatrix }[scene.kind] ndims = len(scene.kind) nscene = len(scene) nscenetot = product(scene.shape[:scene.ndim]) s = cls((ndetectors * ntimes * ndims, nscene * ndims), ncolmax=ncolmax, dtype=synthbeam.dtype, dtype_index=dtype_index, verbose=verbose) index = s.data.index.reshape((ndetectors, ntimes, ncolmax)) c2h = Cartesian2HealpixOperator(nside) if nscene != nscenetot: table = np.full(nscenetot, -1, dtype_index) table[scene.index] = np.arange(len(scene), dtype=dtype_index) def func_thread(i): # e_nf[i] shape: (1, ncolmax, 3) # e_ni shape: (ntimes, ncolmax, 3) e_ni = rotation.T(e_nf[i].swapaxes(0, 1)).swapaxes(0, 1) if nscene != nscenetot: np.take(table, c2h(e_ni).astype(int), out=index[i]) else: index[i] = c2h(e_ni) with pool_threading() as pool: pool.map(func_thread, xrange(ndetectors)) if scene.kind == 'I': value = s.data.value.reshape(ndetectors, ntimes, ncolmax) value[...] = vals[:, None, :] shapeout = (ndetectors, ntimes) else: if str(dtype_index) not in ('int32', 'int64') or \ str(synthbeam.dtype) not in ('float32', 'float64'): raise TypeError( 'The projection matrix cannot be created with types: {0} a' 'nd {1}.'.format(dtype_index, synthbeam.dtype)) func = 'matrix_rot{0}d_i{1}_r{2}'.format(ndims, dtype_index.itemsize, synthbeam.dtype.itemsize) getattr(flib.polarization, func)(rotation.data.T, direction.T, s.data.ravel().view(np.int8), vals.T) if scene.kind == 'QU': shapeout = (ndetectors, ntimes, 2) else: shapeout = (ndetectors, ntimes, 3) return ProjectionOperator(s, shapeout=shapeout)
def _get_projection_operator( rotation, scene, nu, position, synthbeam, horn, primary_beam, verbose=True): if len(position.shape) == 2: position = position[None, ...] ndetectors = position.shape[0] npoints = position.shape[1] ntimes = rotation.data.shape[0] nside = scene.nside thetas, phis, vals = MultiQubicInstrument._peak_angles( scene, nu, position, synthbeam, horn, primary_beam) ncolmax = thetas.shape[-1] thetaphi = _pack_vector(thetas, phis) # (ndetectors, npoints, ncolmax, 2) direction = Spherical2CartesianOperator('zenith,azimuth')(thetaphi) e_nf = direction[..., None, :, :] if nside > 8192: dtype_index = np.dtype(np.int64) else: dtype_index = np.dtype(np.int32) cls = {'I': FSRMatrix, 'QU': FSRRotation2dMatrix, 'IQU': FSRRotation3dMatrix}[scene.kind] ndims = len(scene.kind) nscene = len(scene) nscenetot = product(scene.shape[:scene.ndim]) s = cls((ndetectors * npoints * ntimes * ndims, nscene * ndims), ncolmax=ncolmax, dtype=synthbeam.dtype, dtype_index=dtype_index, verbose=verbose) index = s.data.index.reshape((ndetectors, npoints, ntimes, ncolmax)) c2h = Cartesian2HealpixOperator(nside) if nscene != nscenetot: table = np.full(nscenetot, -1, dtype_index) table[scene.index] = np.arange(len(scene), dtype=dtype_index) e_nf = e_nf.reshape(-1, 1, ncolmax, 3) index = index.reshape(-1, ntimes, ncolmax) def func_thread(i): # e_nf[i] shape: (1, ncolmax, 3) # e_ni shape: (ntimes, ncolmax, 3) e_ni = rotation.T(e_nf[i].swapaxes(0, 1)).swapaxes(0, 1) if nscene != nscenetot: np.take(table, c2h(e_ni).astype(int), out=index[i]) else: index[i] = c2h(e_ni) with pool_threading() as pool: pool.map(func_thread, xrange(ndetectors * npoints)) e_nf = e_nf.reshape(ndetectors, npoints, 1, ncolmax, 3) index = index.reshape(ndetectors, npoints, ntimes, ncolmax) if scene.kind == 'I': value = s.data.value.reshape( ndetectors, npoints, ntimes, ncolmax) value[...] = vals[..., None, :] shapeout = (ndetectors, npoints, ntimes) else: if str(dtype_index) not in ('int32', 'int64') or \ str(synthbeam.dtype) not in ('float32', 'float64'): raise TypeError( 'The projection matrix cannot be created with types:' '{0} and {1}.'.format(dtype_index, synthbeam.dtype)) direction_ = direction.reshape( ndetectors * npoints, ncolmax, 3) vals_ = vals.reshape(ndetectors * npoints, ncolmax) func = 'matrix_rot{0}d_i{1}_r{2}'.format( ndims, dtype_index.itemsize, synthbeam.dtype.itemsize) getattr(flib.polarization, func)( rotation.data.T, direction_.T, s.data.ravel().view(np.int8), vals_.T) if scene.kind == 'QU': shapeout = (ndetectors, npoints, ntimes, 2) else: shapeout = (ndetectors, npoints, ntimes, 3) return ProjectionOperator(s, shapeout=shapeout)