11import importlib
2- import numpy as np
3- from numba import njit , jit , objmode , literal_unroll , types
4- from numba .extending import intrinsic
2+ import inspect
53import numba
6- import mcdc .object_ .numba_types as type_
7- import mcdc .transport .particle_bank as particle_bank_module
4+ import numpy as np
85
6+ from numba import njit , jit , types
7+ from numba .extending import intrinsic
98
109if importlib .util .find_spec ("harmonize" ) is None :
1110 HAS_HARMONIZE = False
1413
1514 HAS_HARMONIZE = True
1615
16+ ####
1717
18- import math
19- import inspect
2018
21- from mcdc .print_ import print_error
19+ import mcdc .config as config
2220
23- import mcdc .code_factory . adapt as adapt
21+ from mcdc .print_ import print_error
2422
2523
2624# =============================================================================
@@ -75,6 +73,17 @@ def codegen(context, builder, signature, args):
7573 return sig , codegen
7674
7775
76+ @njit ()
77+ def uintp_to_voidptr (value ):
78+ val = numba .uintp (value )
79+ return cast_uintp_to_voidptr (val )
80+
81+
82+ @njit ()
83+ def voidptr_to_uintp (value ):
84+ return cast_voidptr_to_uintp (value )
85+
86+
7887def leak (arg ):
7988 pass
8089
@@ -100,10 +109,10 @@ def impl(arg):
100109# =============================================================================
101110
102111
103- """
104112def local_array (shape , dtype ):
105113 return np .zeros (shape , dtype = dtype )
106114
115+
107116@numba .extending .type_callable (local_array )
108117def type_local_array (context ):
109118
@@ -252,7 +261,7 @@ def builtin_local_array(context, builder, sig, args):
252261 raise numba .core .errors .UnsupportedError (
253262 f"Unsupported target context { context } ."
254263 )
255- """
264+
256265
257266# =============================================================================
258267# Decorators
@@ -402,11 +411,17 @@ def nopython_mode(is_on):
402411# =============================================================================
403412
404413
414+ @numba .njit ()
415+ def alloc_bytes_placeholder (size ):
416+ return uintp_to_voidptr (0 )
417+
418+
405419SIMPLE_ASYNC = True
406420
407421none_type = None
408422mcdc_global_type = None
409423mcdc_data_type = None
424+ mcdc_shared_type = None
410425state_spec = None
411426mcdc_global_gpu = None
412427mcdc_data_gpu = None
@@ -417,26 +432,41 @@ def nopython_mode(is_on):
417432step_async = None
418433halt_early = None
419434find_cell_async = None
435+ tally_width = None
436+ tally_length = None
437+ tally_size = None
438+ alloc_managed_bytes = alloc_bytes_placeholder
439+ alloc_device_bytes = alloc_bytes_placeholder
440+ tally_shape_literal = None
420441
421442
422- def gpu_forward_declare (args ):
443+ def gpu_forward_declare (args , tally_shape ):
423444
424445 if args .gpu_rocm_path != None :
425446 harm .config .set_rocm_path (args .gpu_rocm_path )
426447
427448 if args .gpu_cuda_path != None :
428449 harm .config .set_cuda_path (args .gpu_cuda_path )
429450
430- global none_type , mcdc_global_type , mcdc_data_type
451+ global none_type , mcdc_global_type , mcdc_data_type , mcdc_shared_type
431452 global state_spec
432453 global mcdc_global_gpu , mcdc_data_gpu
433454 global group_gpu , thread_gpu
434- global particle_gpu , particle_data_gpu
455+ global particle_gpu , particle_record_gpu
435456 global step_async , find_cell_async , halt_early
457+ global tally_width , tally_length , tally_size
458+
459+ tally_size = tally_shape [0 ] * tally_shape [1 ] * 8
460+
461+ global tally_shape_literal
462+ tally_shape_literal = tally_shape
436463
437464 none_type = numba .from_dtype (np .dtype ([]))
438- mcdc_global_type = numba .from_dtype (type_ .simulation )
439- mcdc_data_type = numba .from_dtype (type_ .tally )
465+ mcdc_global_type = numba .types .Array (numba .from_dtype (type_ .global_ ), (1 ,), "C" )
466+ # mcdc_global_type = numba.from_dtype(type_.global_)
467+
468+ tally_dims = len (tally_shape )
469+ mcdc_data_type = numba .types .Array (numba .float64 , tally_dims , "C" )
440470 state_spec = (
441471 {
442472 "global" : mcdc_global_type ,
@@ -446,12 +476,12 @@ def gpu_forward_declare(args):
446476 none_type ,
447477 )
448478 access_fns = harm .RuntimeSpec .access_fns (state_spec )
449- mcdc_global_gpu = access_fns ["device" ]["global" ]
450- mcdc_data_gpu = access_fns ["device" ]["data" ]
479+ mcdc_global_gpu = access_fns ["device" ]["global" ][ "indirect" ]
480+ mcdc_data_gpu = access_fns ["device" ]["data" ][ "direct" ]
451481 group_gpu = access_fns ["group" ]
452482 thread_gpu = access_fns ["thread" ]
453483 particle_gpu = numba .from_dtype (type_ .particle )
454- particle_data_gpu = numba .from_dtype (type_ .particle_data )
484+ particle_record_gpu = numba .from_dtype (type_ .particle_record )
455485
456486 def step (prog : numba .uintp , P : particle_gpu ):
457487 pass
@@ -463,6 +493,44 @@ def find_cell(prog: numba.uintp, P: particle_gpu):
463493 interface = adapt .harm .RuntimeSpec .program_interface ()
464494 halt_early = interface ["halt_early" ]
465495
496+ global alloc_managed_bytes
497+ global alloc_device_bytes
498+ alloc_managed_bytes = harm .alloc_managed_bytes
499+ alloc_device_bytes = harm .alloc_device_bytes
500+
501+
502+ # =============================================================================
503+ # Global GPU/CPU Arry Variable Constructors
504+ # =============================================================================
505+
506+
507+ def create_data_array (size , dtype ):
508+ if config .target == "gpu" :
509+ if config .gpu_state_storage == "managed" :
510+ data_tally_ptr = alloc_managed_bytes (tally_size )
511+ else :
512+ data_tally_ptr = alloc_device_bytes (tally_size )
513+ data_tally_uint = voidptr_to_uintp (data_tally_ptr )
514+ data_tally = numba .carray (data_tally_ptr , (width , length ), type_ .float64 )
515+ return data_tally , data_tally_uint
516+ else :
517+ data_tally = np .zeros (size , dtype = dtype )
518+ return data_tally , 0
519+
520+
521+ def create_mcdc_array (dtype ):
522+ if config .target == "gpu" :
523+ if config .gpu_state_storage == "managed" :
524+ mcdc_ptr = alloc_managed_bytes (type_ .global_size )
525+ else :
526+ mcdc_ptr = alloc_device_bytes (type_ .global_size )
527+ mcdc_uint = voidptr_to_uintp (mcdc_ptr )
528+ mcdc_array = numba .carray (mcdc_ptr , (1 ,), type_ .global_ )
529+ return mcdc_array , mcdc_uint
530+ else :
531+ mcdc_array = np .zeros ((1 ,), dtype = dtype )
532+ return mcdc_array , 0
533+
466534
467535# =============================================================================
468536# Seperate GPU/CPU Functions to Target Different Platforms
@@ -514,51 +582,14 @@ def thread(prog):
514582
515583
516584@for_cpu ()
517- def add_active (particle , prog ):
518- particle_bank_module .add_particle (particle , prog ["bank_active" ])
519-
520-
521- @for_gpu ()
522- def add_active (P_reclike , prog ):
523- P = local_array (1 , type_ .particle )
524- particle_bank_module .recordlike_to_particle (P , P_reclike )
525- if SIMPLE_ASYNC :
526- step_async (prog , P [0 ])
527- else :
528- find_cell_async (prog , P [0 ])
529-
530-
531- @for_cpu ()
532- def add_source (particle , prog ):
533- particle_bank_module .add_particle (particle , prog ["bank_source" ])
534-
535-
536- @for_gpu ()
537- def add_source (particle , prog ):
538- mcdc = mcdc_global (prog )
539- particle_bank_module .add_particle (particle , mcdc ["bank_source" ])
540-
541-
542- @for_cpu ()
543- def add_census (particle , prog ):
544- particle_bank_module .add_particle (particle , prog ["bank_census" ])
545-
546-
547- @for_gpu ()
548- def add_census (particle , prog ):
549- mcdc = mcdc_global (prog )
550- particle_bank_module .add_particle (particle , mcdc ["bank_census" ])
551-
552-
553- @for_cpu ()
554- def add_future (particle , prog ):
555- particle_bank_module .add_particle (particle , prog ["bank_future" ])
585+ def add_IC (P_arr , prog ):
586+ particle_bank .add_particle (P_arr , prog ["technique" ]["IC_bank_neutron_local" ])
556587
557588
558589@for_gpu ()
559- def add_future ( particle , prog ):
590+ def add_IC ( P_arr , prog ):
560591 mcdc = mcdc_global (prog )
561- particle_bank_module .add_particle (particle , mcdc ["bank_future " ])
592+ particle_bank .add_particle (P_arr , mcdc ["technique" ][ "IC_bank_neutron_local " ])
562593
563594
564595@for_cpu ()
0 commit comments