Skip to content

Commit

Permalink
geocode: add OMP_NUM_THREADS control
Browse files Browse the repository at this point in the history
+ add geocode/check_num_processor() to check OMP_NUM_THREADS if it's defined in environment variable and use it if --nprocs is not specified.

+ set default --nprocs to 1 because 1) bonding between pykdtree and openmp is not clear yet and 2) the speedup is negligible for macport install and slows down for conda install

+ change default nprocs value to 1 in objects/resample.py

+ detect lookup table file in current directory besides the INPUTS directory.
  • Loading branch information
yunjunz committed May 3, 2019
1 parent 406c823 commit a9ec91b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
40 changes: 32 additions & 8 deletions pysar/geocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def create_parser():
parser.add_argument('--fill', dest='fillValue', type=float, default=np.nan,
help='Value used for points outside of the interpolation domain.\n' +
'Default: np.nan')
parser.add_argument('-n','--nprocs', dest='nprocs', type=int,
help='number of processors to be used for calculation.\n' +
parser.add_argument('-n','--nprocs', dest='nprocs', type=int, default=1,
help='number of processors to be used for calculation. Default: 1\n' +
'Note: Do not use more processes than available processor cores.')
parser.add_argument('-p','--processor', dest='processor', type=str, choices={'pyresample', 'scipy'},
help='processor module used for interpolation.')
Expand All @@ -88,6 +88,12 @@ def create_parser():
def cmd_line_parse(iargs=None):
parser = create_parser()
inps = parser.parse_args(args=iargs)

if inps.templateFile:
inps = read_template2inps(inps.templateFile, inps)

inps = _check_inps(inps)

return inps


Expand Down Expand Up @@ -119,6 +125,7 @@ def _check_inps(inps):
if None in inps.laloStep:
inps.laloStep = None

inps.nprocs = check_num_processor(inps.nprocs)
return inps


Expand Down Expand Up @@ -155,6 +162,26 @@ def read_template2inps(template_file, inps):


############################################################################################
def check_num_processor(nprocs):
"""Check number of processors
Note by Yunjun, 2019-05-02:
1. conda install pyresample will install pykdtree and openmp, but it seems not working
geocode.py is getting slower with more processors
2. macports seems to have minor speedup when more processors
Thus, default number of processors is set to 1; although the capability of using multiple
processors is written here.
"""
if not nprocs:
#OMP_NUM_THREADS is defined in environment variable for OpenMP
if 'OMP_NUM_THREADS' in os.environ:
nprocs = int(os.getenv('OMP_NUM_THREADS'))
else:
nprocs = int(multiprocessing.cpu_count() / 2)
nprocs = min(multiprocessing.cpu_count(), nprocs)
print('number of processor to be used: {}'.format(nprocs))
return nprocs


def metadata_radar2geo(atr_in, res_obj, print_msg=True):
"""update metadata for radar to geo coordinates"""
atr = dict(atr_in)
Expand Down Expand Up @@ -249,9 +276,6 @@ def run_geocode(inps):
processor=inps.processor)
res_obj.open()

if not inps.nprocs:
inps.nprocs = multiprocessing.cpu_count()

# resample input files one by one
for infile in inps.file:
print('-' * 50+'\nresampling file: {}'.format(infile))
Expand All @@ -275,8 +299,11 @@ def run_geocode(inps):
else:
data, atr = readfile.read(infile, datasetName=dsName, print_msg=False)

# keep timeseries data as 3D matrix when there is only one acquisition
# because readfile.read() will squeeze it to 2D
if atr['FILE_TYPE'] == 'timeseries' and len(data.shape) == 2:
data = np.reshape(data, (1, data.shape[0], data.shape[1]))

res_data = res_obj.run_resample(src_data=data,
interp_method=inps.interpMethod,
fill_value=inps.fillValue,
Expand All @@ -303,9 +330,6 @@ def run_geocode(inps):
######################################################################################
def main(iargs=None):
inps = cmd_line_parse(iargs)
if inps.templateFile:
inps = read_template2inps(inps.templateFile, inps)
inps = _check_inps(inps)

run_geocode(inps)
return
Expand Down
12 changes: 6 additions & 6 deletions pysar/objects/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def open(self):
elif self.processor == 'scipy' and 'Y_FIRST' in self.lut_metadata.keys():
self.prepare_regular_grid_interpolator()

def run_resample(self, src_data, interp_method='nearest', fill_value=np.nan, nprocs=None, print_msg=True):
def run_resample(self, src_data, interp_method='nearest', fill_value=np.nan, nprocs=1, print_msg=True):
"""Run interpolation operation for input 2D/3D data
Parameters: src_data : 2D/3D np.array, source data to be geocoded
interp_method : string, nearest | linear
Expand Down Expand Up @@ -355,7 +355,7 @@ def get_segment_number(self, unit_size=1e6):
num_segment = int(self.dest_def.size / unit_size + 0.5)
return num_segment

def run_pyresample(self, src_data, interp_method='nearest', fill_value=np.nan, nprocs=None,
def run_pyresample(self, src_data, interp_method='nearest', fill_value=np.nan, nprocs=1,
radius=None, print_msg=True):
"""
Resample input src_data into dest_data
Expand All @@ -377,17 +377,17 @@ def run_pyresample(self, src_data, interp_method='nearest', fill_value=np.nan, n
else:
radius = self.get_radius_of_influence()

if not nprocs:
nprocs = multiprocessing.cpu_count()

num_segment = self.get_segment_number()

if interp_method.startswith('near'):
if print_msg:
print('nearest resampling with kd_tree ({} segments) ...'.format(num_segment))
msg = 'nearest resampling with kd_tree '
msg += 'using {} processor cores in {} segments ...'.format(nprocs, num_segment)
print(msg)
dest_data = pr.kd_tree.resample_nearest(self.src_def,
src_data,
self.dest_def,
nprocs=nprocs,
fill_value=fill_value,
radius_of_influence=radius,
segments=num_segment,
Expand Down
2 changes: 1 addition & 1 deletion pysar/utils/utils1.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def get_lookup_file(filePattern=None, abspath=False, print_msg=True):
'geometryGeo_tight.h5', 'geometryGeo.h5',
'geomap*lks_tight.trans', 'geomap*lks.trans',
'sim*_tight.UTM_TO_RDC', 'sim*.UTM_TO_RDC']
filePattern = [os.path.join('INPUTS', i) for i in filePattern]
filePattern = [os.path.join('INPUTS', i) for i in filePattern] + filePattern
existFiles = []
try:
existFiles = get_file_list(filePattern)
Expand Down

0 comments on commit a9ec91b

Please sign in to comment.