def solve_tridiagonal_matrix_c_callback(self, right_hand_side, current_x_index, current_L, dt): # right_hand_side comes in as a normal C double pointer.. this needed to # converted (not so much converted.. just 'dressed up') as a Numpy array.. # luckily this is easy and cheap (althought there are faster ways.. but # worth dealing with since this not a code bottleneck) # the following line caused a memoery leak upon each call and was replaced.. I need to satify myself why. #b = numpy.ctypeslib.as_array( right_hand_side, shape = (self.num_pr_cells, ) ) import ctypes b = np.ctypeslib.as_array( (ctypes.c_double * self.num_pr_cells).from_address( ctypes.addressof(right_hand_side.contents))) try: temp = self.solve_tridiagonal_matrix(self.Alpha, b) b[:] = temp[:] except: PRINT( "Error in Solve Tridiagonal Matrix Callback (spatial cell=%d, L=%d, dt=%0.3e) \n" % (current_x_index, current_L, dt)) PRINT( "\t The Exception thrown was: \n --------------------------------- \n" ) PRINT(traceback.format_exc()) PRINT("") exit(-1) return -1 return 0 """
def printCConfigurationHeader(): PRINT("") PRINT("-------C/cpu confiugration info--------") PRINT("Pointer size (in bytes): %d " % (getPointerSizeInBytes())) PRINT("Int size (in bytes): %d " % (getIntSizeInBytes())) PRINT("Long size (in bytes): %d " % (getLongSizeInBytes())) PRINT("LongLong size (in bytes): %d " % (getLongLongSizeInBytes())) PRINT("Float size (in bytes): %d " % (getFloatSizeInBytes())) PRINT("Double size (in bytes): %d " % (getDoubleSizeInBytes())) PRINT("---------------------------------------") PRINT("")
def profile_main_loop_start(local_state, simulation_state): if platform.system().lower() != 'windows': if (statprof == None): PRINT( "Note: Profiling disabled. Unable to load the 'statprof' Python module. Install 'statprof'." ) return statprof.start() else: PRINT("Note: Profiling disabled when running under Windows.")
def solve_full_matrix_c_callback(self, right_hand_side, current_x_index, current_L, dt): #PRINT("in solve_full_matrix_c_callback FULL!!!!!!!"\) import ctypes try: # the following line caused a memoery leak upon each call and was replaced.. I need to satify myself why. #b = numpy.ctypeslib.as_array( right_hand_side, shape = (self.num_pr_cells, ) ) b = np.ctypeslib.as_array( (ctypes.c_double * self.num_pr_cells).from_address( ctypes.addressof(right_hand_side.contents))) temp = self.solve_matrix(self.Alpha, b) b[:] = temp except: PRINT( "Error in Solve Full Solver Matrix Callback (spatial cell=%d, L=%d, dt=%0.3e) \n" % (current_x_index, current_L, dt)) PRINT( "\t The Exception thrown was: \n --------------------------------- \n" ) PRINT(traceback.format_exc()) PRINT("") exit(-1) return -1 return 0
def filter_statprof(statprof): # only report the profiling for the Root node.. that's almost always what is needed. if mpi_info.is_I_root == False: return if platform.system().lower() != 'windows': if (statprof == None): PRINT( "Note: Profiling disabled. Unable to load the 'statprof' Python module. Install 'statprof'." ) return else: PRINT( "Note: Profiling disabled when running under Windows because 'statprof' does not work." ) return pass PRINT("Final Profiling Info") PRINT("-----------------------------------") PRINT("\t Include filters:") filtersInclude = None if filtersForFiltersInclude != None and len(filtersForFiltersInclude) > 0: filtersInclude = [] for filter in filtersForFiltersInclude: PRINT("\t\t %s" % (str(filter), )) filtersInclude.append(str(filter)) else: PRINT("\t\t None") PRINT("\t Exclude filters:") filtersExclude = None if filtersForFiltersExclude != None and len(filtersForFiltersExclude) > 0: filtersExclude = [] for filter in filtersForFiltersExclude: PRINT("\t\t %s" % (str(filter), )) filtersExclude.append(str(filter)) else: PRINT("\t\t None") PRINT("") PRINT("") outp = open('profile_data.txt', 'w') statprof.display(fp=outp) outp.close() inp = open('profile_data.txt', 'r') lines = inp.readlines() inp.close() error_lines = [] profile_info = [] line_number = -1 for line in lines: line_number += 1 # filter (include filter) if filtersInclude != None: found = False for name in filtersInclude: if line.find(name) > -1: found = True break if found == False: continue pass # filter (exclude filter) if filtersExclude != None: found = False for name in filtersExclude: if line.find(name) != -1: found = True if found == True: continue pass if line_number > 1: try: numbers = re.findall( r"([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)", line) percent_time = float(numbers[0][0]) cum_seconds = float(numbers[1][0]) self_seconds = float(numbers[2][0]) text = re.search(r"([a-zA-Z][\.\w\:]+)", line) info = text.group() #sort(key=lambda tup: tup[1]) profile_info.append( (percent_time, cum_seconds, self_seconds, info)) except: error_lines.append(line) profile_info.sort(key=lambda sme: sme[0]) for line in profile_info: PRINT("%f \t %f \t %f \t %s" % (line[0], line[1], line[2], line[3])) if len(error_lines) > 0: PRINT("---------------------") PRINT("Error lines!") PRINT("") for line in error_lines: PRINT("\t %s" % line) PRINT("") PRINT( "Raw output in file 'profile_data.txt' and CSV data in 'profile.csv'") PRINT("") outp = open('profile_data.csv', 'w') for line in profile_info: outp.write("%f,%f,%f,%s\n" % (line[0], line[1], line[2], line[3])) outp.close()
# of statprof (pip install statprof) and does NOT work under windows. # for now, we explictly reisgter this conponent.. we can make it cooler later import OshunEventQueue import platform from OshunMpi import mpi_info from OshunGlobalState import PRINT as PRINT import re if platform.system().lower() != 'windows': try: import statprof except: PRINT( "Note: Profiling disabled. Unable to load the 'statprof' Python module. Install 'statprof'." ) statprof = None else: PRINT( "Note: Profiling disabled when running under Windows because 'statprof' does not work." ) PRINT( "Note: Profiling disabled. Unable to load the 'statprof' Python module." ) statprof = None filtersForFiltersInclude = None filtersForFiltersExclude = None
def quick_compare_dist(self, F1, F2): for ip in xrange(0, self.num_pr_cells): if F1[ip] != F2[ip]: PRINT("They are unequal!") exit(-1) return
def __init__(self, species, state, CFG, use_c_version=False): """ tried python cpp, python """ # configure the FP code (pure python or CPP or GPU etc..) self.explicit__execution_config__python = False self.explicit__execution_config__cpp = True self.implicit_calc_flm__run_all_in_cpp = True self.explicit_calc_f00__run_all_in_cpp = True self.implicit_advance__execution_config__python = False self.implicit_advance__execution_config__cpp = True self.test_cpp_vs_python_marix = False self.implicit_reset_coeff___execution_config__python = False self.implicit_reset_coeff__execution_config__cpp = True if use_c_version: self.explicit_calc_f00__run_all_in_cpp = True self.implicit_calc_flm__run_all_in_cpp = True self.implicit_advance__execution_config__cpp = True self.implicit_reset_coeff__execution_config__cpp = True self.explicit__execution_config__cpp = True else: self.explicit_calc_f00__run_all_in_cpp = False self.implicit_calc_flm__run_all_in_cpp = False self.explicit__execution_config__python = True self.explicit__execution_config__cpp = False self.implicit_advance__execution_config__python = True self.implicit_advance__execution_config__cpp = False self.test_cpp_vs_python_marix = False self.implicit_reset_coeff___execution_config__python = True self.implicit_reset_coeff__execution_config__cpp = False if mpi_info.rank == 0: PRINT("") PRINT("Fokker Planck Execution ") PRINT("Implict Solve Mode Uses Tridiagonal Approximation: %s" % (str(CFG.if_tridiagonal))) PRINT("----------------------------------------------------------") if self.explicit__execution_config__python: self.set_run_type__fp_explicit_python() PRINT("\t'Explicit' Portion of Collsions: PYTHON") else: self.set_run_type__fp_explicit_cpp() PRINT("\t'Explicit' Portion of Collsions: CPP") self.calc_f00 = self.calc_f00__python if self.explicit_calc_f00__run_all_in_cpp: self.calc_f00 = fokker_plank__calc_f00__cpp PRINT( "\t'Explicit' Portion of Collsions: TAKEN ALL OVER BY CPP. All other flags invalidated." ) self.calc_flm = self.calc_flm__python self.advance_1 = self.euler_backward_solver_for_fp_advance_1 self.advance_flm = self.euler_backward_solver_for_fp_advance_lm if self.implicit_calc_flm__run_all_in_cpp: self.calc_flm = fokker_plank__calc_flm__cpp self.advance_1 = fokker_plank__advance_1__cpp self.advance_flm = fokker_plank__advance_flm__cpp PRINT( "\t'Implicit' Portion of Collsions: TAKEN ALL OVER BY CPP. All other flags invalidated." ) if self.implicit_reset_coeff___execution_config__python: self.set_run_type__fp_implicit_reset_coeff__python() PRINT("\t'Implict Coeff Reset' Portion of Collsions: PYTHON") else: self.set_run_type__fp_implicit_reset_coeff__cpp() PRINT("\t'Implict Coeff Reset' Portion of Collsions: CPP") if self.implicit_advance__execution_config__python: PRINT("\t'Implict Advance' Portion of Collsions: PYTHON") else: PRINT("\t'Implict Advance' Portion of Collsions': CPP") if self.implicit_calc_flm__run_all_in_cpp: self.printStatus = self.printStatusCpp else: self.printStatus = self.printStatusPython PRINT("----------------------------------------------------------") PRINT("") PRINT("") self.ttt = 0 # variables for the RK4 part.. self.F0 = np.zeros(species.momentum_axis.num_points) self.Fh = np.zeros(species.momentum_axis.num_points) self.F1 = np.zeros(species.momentum_axis.num_points) #TODO: get rid of this.. it just for debugging self.Ftemp = np.zeros(species.momentum_axis.num_points) # these is a 'pointer' (view) to the |p| of the l=0,m=0 mode for the current spatial cell we are looking at # THIS is only for debugging.... #self.data_for_current_spatial_cell = species.F.distribution_function_data #self.debug_f00_data_for_current_spatial_cell = self.species.F.distribution_function_data[ #TODO: talk to Micheal and make this more robust.. this should be sperated out into # A global, specialized module.. I hate how it is spread out in things like Osiris. # Since this uses RK4, then all the code modules must use atleast 4 boundry cells. # TODO: read these from the CFG object. #self.explicit_solver_order = 3 self.NB = int(species.F.num_boundry_cells[0][0]) self.low_P_transition_cell = 4 self.num_plain_cells = np.copy(species.F.num_plain_cells) self.num_total_cells = np.copy(species.F.num_total_cells) self.num_bounrdry_cells = np.copy(species.F.num_total_cells) self.density_np = species.species_config.density_np self.zeta = species.species_config.zeta self.is_tridiagonal = CFG.if_tridiagonal self.is_implicit1D = CFG.if_implicit1D self.num_l = species.num_l self.num_m = species.num_m #TODO: talk to Micheal.. centralize the time handling.. porlly not right # to be simply reading from input object. self.num_subcycling_steps = int(CFG.dt / CFG.small_dt) + 1 self.subcycle_dt = CFG.dt / float(self.num_subcycling_steps) # this is the timestep.. self.h = self.subcycle_dt self.species = species # TODO: check boundry cells or else.. # setup inital stuffs for the FP explicit calcuation... self.vr = np.zeros(species.momentum_axis.num_points) self.U4 = np.zeros(species.momentum_axis.num_points) self.U4m1 = np.zeros(species.momentum_axis.num_points) self.U2 = np.zeros(species.momentum_axis.num_points) self.U2m1 = np.zeros(species.momentum_axis.num_points) self.U1 = np.zeros(species.momentum_axis.num_points) self.U1m1 = np.zeros(species.momentum_axis.num_points) self.J1 = np.zeros(species.momentum_axis.num_points) #self.I2 = np.zeros( species.momentum_axis.num_points ) self.I4 = np.zeros(species.momentum_axis.num_points) self.U3 = np.zeros(species.momentum_axis.num_points) self.Qn = np.zeros(species.momentum_axis.num_points) self.Pn = np.zeros(species.momentum_axis.num_points) self.re = 2.8179402894e-13 self.kp = np.sqrt(4.0 * np.pi * self.density_np * self.re) self.c_kpre = 4.0 * np.pi / 3.0 * self.re * self.kp #PRINT("FOKKERPLANK: kp=%e, c_kpre=%e" % (self.kp, self.c_kpre)) # shorten some nmaes teroparlily to makes things prettier pr_axis = species.momentum_axis vr = self.vr self.num_pr_cells = pr_axis.num_points #PRINT(species.momentum_axis.values) # initalize all the needed constants.. for i in xrange(0, pr_axis.num_points): self.vr[i] = pr_axis.values[i] self.vr[i] = self.vr[i] / (np.sqrt(1 + self.vr[i] * self.vr[i])) for i in xrange(1, pr_axis.num_points): self.U4[i] = 0.5 * pow(vr[i], 4) * (vr[i] - vr[i - 1]) self.U4m1[i] = 0.5 * pow(vr[i - 1], 4) * (vr[i] - vr[i - 1]) self.U2[i] = 0.5 * vr[i] * vr[i] * (vr[i] - vr[i - 1]) self.U2m1[i] = 0.5 * vr[i - 1] * vr[i - 1] * (vr[i] - vr[i - 1]) self.U1[i] = 0.5 * vr[i] * (vr[i] - vr[i - 1]) self.U1m1[i] = 0.5 * vr[i - 1] * (vr[i] - vr[i - 1]) for i in xrange(0, pr_axis.num_points): self.U3[i] = pow(vr[i], 3) self.Qn[0] = 1.0 / ((vr[0] * vr[0] * vr[1]) / 2.0) for i in xrange(1, pr_axis.num_points - 1): self.Qn[i] = 1.0 / (vr[i] * vr[i] * (vr[i + 1] - vr[i - 1]) / 2.0) for i in xrange(0, pr_axis.num_points - 1): self.Pn[i] = 1.0 / ((vr[i + 1] - vr[i]) / 2.0 * (vr[i + 1] + vr[i])) self.G_constant_1 = (vr[0] * vr[0]) / (vr[1] * vr[1]) self.G_constant_vr3 = np.power(vr, 3) self.G_constant_vr5 = np.power(vr, 5) self.G_constant_vr5_2 = np.power(vr, 5) self.G_constant_vr7 = np.power(vr, 7) self.G_constant_vr5 *= 0.2 self.G_constant_vr5_2 *= (0.2 / (vr[1] * vr[1])) self.G_constant_vr7 *= (1.0 / (vr[1] * vr[1] * 7.0)) #------ Stuffs for the implict, higher harmonic calcuations... self.kpre = self.re * self.kp self.J1m = np.zeros(species.momentum_axis.num_points) self.I0 = np.zeros(species.momentum_axis.num_points) self.I2 = np.zeros(species.momentum_axis.num_points) self.Scattering_Term = np.zeros(species.momentum_axis.num_points) self.df0 = np.zeros(species.momentum_axis.num_points) self.ddf0 = np.zeros(species.momentum_axis.num_points) self.TriI1 = np.zeros(species.momentum_axis.num_points) self.TriI2 = np.zeros(species.momentum_axis.num_points) self.vr3 = np.zeros(species.momentum_axis.num_points) for i in xrange(0, pr_axis.num_points): self.vr3[i] = (self.vr[i] * self.vr[i] * self.vr[i]) # TODO: can get rid of this.. #self.fout = np.zeros ( species.momentum_axis.num_points ) temp = np.zeros((3, pr_axis.num_points)) self.tridiagonal_solver_temp = np.matrix(temp) # matricies! # TODO: cehck for any hidden reason.. and make this pcart more tranparent. # why did I do the 'temp1=np.zeros(...,..) stuff? temp1 = np.zeros((pr_axis.num_points, pr_axis.num_points)) self.Alpha_Tri = np.matrix(temp1) temp2 = np.zeros((pr_axis.num_points, pr_axis.num_points)) self.Alpha = np.matrix(temp2) # these are purly denugging variables.... # used to redirect the solving routines for comparison to python # can be taken out! if self.test_cpp_vs_python_marix: temp3 = np.zeros((pr_axis.num_points, pr_axis.num_points)) self.Alpha_debug = np.matrix(temp3) # some misc precomputations..... self.IvDnDm1 = np.zeros(self.num_pr_cells) self.IvDnDp1 = np.zeros(self.num_pr_cells) self.Ivsq2Dn = np.zeros(self.num_pr_cells) self.f00_factor = ((vr[0] * vr[0]) / (vr[1] * vr[1])) # These are calculated in the implict part of the FP code.. (reset_coeff) and are used # across function calls.. so if we using c++, we need to set these. self._LOGee = 0.0 self._ZLOGei = 0.0 # slush array for solver to put various stats into. self.solverStats = np.zeros(10) for i in xrange(1, self.num_pr_cells - 1): # some common 'constants' used in tthe implicy marriage cerimony self.IvDnDm1[i] = 1.0 / (vr[i] * 0.5 * (vr[i + 1] - vr[i - 1]) * (vr[i] - vr[i - 1]) ) # ( v*D_n*D_{n-1/2} )^(-1) self.IvDnDp1[i] = 1.0 / (vr[i] * 0.5 * (vr[i + 1] - vr[i - 1]) * (vr[i + 1] - vr[i]) ) # ( v*D_n*D_{n+1/2} )^(-1) self.Ivsq2Dn[i] = 1.0 / (vr[i] * vr[i] * (vr[i + 1] - vr[i - 1]) ) # ( v^2 * 2*D_n )^(-1) if use_c_version: # TODO move this.. i want to keep all ctypes things seperate... # create helper objects that allow C/C++ to easily call pyhton functions (they are called 'callbacks') # These will be used for now as a (lazy) compremise where I am using # numpy matrix solvers rather then linking to pur c/c++ ones.. I'll # get to thi later.. it effect perfromace at all. # self.tridiag_solver_callback = matrix_solver_callback_template( self.solve_tridiagonal_matrix_c_callback) self.full_matrix_solver_callback = matrix_solver_callback_template( self.solve_full_matrix_c_callback) self.debug_matrix_callback = debug_callback_template( self.debug_matrix_solver_callback) init__fokker_plank_implicit__cpp( self.vr, self.vr3, self.df0, self.ddf0, self.Scattering_Term, self.Alpha, self.Alpha_Tri, self.J1m, self.TriI1, self.TriI2, self.IvDnDm1, self.IvDnDp1, self.Ivsq2Dn, self.I0, self.kpre, self.zeta, self.f00_factor, self.is_tridiagonal, self.is_implicit1D, self.species.index, self._LOGee, self._ZLOGei, self.tridiag_solver_callback, self.full_matrix_solver_callback, self.debug_matrix_callback, self.solverStats) init__fokker_plank_explicit__cpp( self.vr, self.U4, self.U4m1, self.U2, self.U2m1, self.U1, self.U1m1, self.J1, self.I4, self.U3, self.Qn, self.Pn, self.I2, self.G_constant_1, self.G_constant_vr3, self.G_constant_vr5, self.G_constant_vr5_2, self.G_constant_vr7, self.density_np, self.NB, self.c_kpre, self.num_subcycling_steps, self.species.index) # TAKE OUT NOW: This was one leve back # hack to make timer work well... fokker_plank__implicit__rest_coeff__cpp.implcit_FP_object = self