You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

parallel.py 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Dec 11 11:39:46 2018
  5. Parallel aid functions.
  6. @author: ljia
  7. """
  8. import multiprocessing
  9. from multiprocessing import Pool
  10. from tqdm import tqdm
  11. import sys
  12. def parallel_me(func, func_assign, var_to_assign, itr, len_itr=None, init_worker=None,
  13. glbv=None, method=None, n_jobs=None, chunksize=None, itr_desc=''):
  14. '''
  15. '''
  16. if method == 'imap_unordered':
  17. if glbv: # global varibles required.
  18. # def init_worker(v_share):
  19. # global G_var
  20. # G_var = v_share
  21. with Pool(processes=n_jobs, initializer=init_worker,
  22. initargs=glbv) as pool:
  23. if n_jobs == None:
  24. n_jobs = multiprocessing.cpu_count()
  25. if chunksize == None:
  26. if len_itr < 100 * n_jobs:
  27. chunksize = int(len_itr / n_jobs) + 1
  28. else:
  29. chunksize = 100
  30. for result in tqdm(pool.imap_unordered(func, itr, chunksize),
  31. desc=itr_desc, file=sys.stdout):
  32. func_assign(result, var_to_assign)
  33. else:
  34. with Pool(processes=n_jobs) as pool:
  35. if n_jobs == None:
  36. n_jobs = multiprocessing.cpu_count()
  37. if chunksize == None:
  38. if len_itr < 100 * n_jobs:
  39. chunksize = int(len_itr / n_jobs) + 1
  40. else:
  41. chunksize = 100
  42. for result in tqdm(pool.imap_unordered(func, itr, chunksize),
  43. desc=itr_desc, file=sys.stdout):
  44. func_assign(result, var_to_assign)
  45. def parallel_gm(func, Kmatrix, Gn, init_worker=None, glbv=None,
  46. method='imap_unordered', n_jobs=None, chunksize=None):
  47. from itertools import combinations_with_replacement
  48. def func_assign(result, var_to_assign):
  49. var_to_assign[result[0]][result[1]] = result[2]
  50. var_to_assign[result[1]][result[0]] = result[2]
  51. itr = combinations_with_replacement(range(0, len(Gn)), 2)
  52. len_itr = int(len(Gn) * (len(Gn) + 1) / 2)
  53. parallel_me(func, func_assign, Kmatrix, itr, len_itr=len_itr,
  54. init_worker=init_worker, glbv=glbv, method=method, n_jobs=n_jobs,
  55. chunksize=chunksize, itr_desc='calculating kernels')

A Python package for graph kernels, graph edit distances and graph pre-image problem.