Skip to content
Snippets Groups Projects
Commit 463bb37d authored by Rafael Ravedutti's avatar Rafael Ravedutti
Browse files

Add device allocations

parent 0568f6f8
No related branches found
No related tags found
No related merge requests found
...@@ -31,6 +31,6 @@ psim.periodic(2.8) ...@@ -31,6 +31,6 @@ psim.periodic(2.8)
psim.vtk_output("output/test") psim.vtk_output("output/test")
psim.compute(lj, cutoff_radius, {'sigma6': sigma6, 'epsilon': epsilon}) psim.compute(lj, cutoff_radius, {'sigma6': sigma6, 'epsilon': epsilon})
psim.compute(euler, symbols={'dt': dt}) psim.compute(euler, symbols={'dt': dt})
psim.target(pairs.target_cpu()) #psim.target(pairs.target_cpu())
#psim.target(pairs.target_gpu()) psim.target(pairs.target_gpu())
psim.generate() psim.generate()
...@@ -24,11 +24,17 @@ class FetchModulesReferences(Visitor): ...@@ -24,11 +24,17 @@ class FetchModulesReferences(Visitor):
def visit_Array(self, ast_node): def visit_Array(self, ast_node):
for m in self.module_stack: for m in self.module_stack:
m.add_array(ast_node, self.writing) m.add_array(ast_node, self.writing)
if m.run_on_device:
ast_node.device_flag = True
def visit_Property(self, ast_node): def visit_Property(self, ast_node):
for m in self.module_stack: for m in self.module_stack:
m.add_property(ast_node, self.writing) m.add_property(ast_node, self.writing)
if m.run_on_device:
ast_node.device_flag = True
def visit_Var(self, ast_node): def visit_Var(self, ast_node):
for m in self.module_stack: for m in self.module_stack:
m.add_variable(ast_node, self.writing) m.add_variable(ast_node, self.writing)
if m.run_on_device:
ast_node.device_flag = True
...@@ -25,14 +25,18 @@ from pairs.code_gen.printer import Printer ...@@ -25,14 +25,18 @@ from pairs.code_gen.printer import Printer
class CGen: class CGen:
temp_id = 0 temp_id = 0
def __init__(self, output, debug=False): def __init__(self, output, target, debug=False):
self.sim = None self.sim = None
self.target = None
self.debug = debug self.debug = debug
self.print = Printer(output) self.print = Printer(output)
def assign_simulation(self, sim): def assign_simulation(self, sim):
self.sim = sim self.sim = sim
def assign_target(self, target):
self.target = target
def generate_program(self, ast_node): def generate_program(self, ast_node):
self.print.start() self.print.start()
self.print("#include <math.h>") self.print("#include <math.h>")
...@@ -101,7 +105,7 @@ class CGen: ...@@ -101,7 +105,7 @@ class CGen:
t = ast_node.array.type() t = ast_node.array.type()
tkw = Types.ctype2keyword(t) tkw = Types.ctype2keyword(t)
size = self.generate_expression(BinOp.inline(ast_node.array.alloc_size())) size = self.generate_expression(BinOp.inline(ast_node.array.alloc_size()))
if ast_node.array.is_static() and ast_node.array.init_value is not None: if ast_node.array.init_value is not None:
v_str = str(ast_node.array.init_value) v_str = str(ast_node.array.init_value)
if t == Types.Int64: if t == Types.Int64:
v_str += "LL" v_str += "LL"
...@@ -180,10 +184,12 @@ class CGen: ...@@ -180,10 +184,12 @@ class CGen:
self.print(f"{call};") self.print(f"{call};")
if isinstance(ast_node, CopyToDevice): if isinstance(ast_node, CopyToDevice):
self.print(f"pairs::copy_to_device({ast_node.prop.name()})") array_name = ast_node.prop.name()
self.print(f"pairs::copy_to_device({array_name}, d_{array_name})")
if isinstance(ast_node, CopyToHost): if isinstance(ast_node, CopyToHost):
self.print(f"pairs::copy_to_host({ast_node.prop.name()})") array_name = ast_node.prop.name()
self.print(f"pairs::copy_to_host(d_{array_name}, {array_name})")
if isinstance(ast_node, For): if isinstance(ast_node, For):
iterator = self.generate_expression(ast_node.iterator) iterator = self.generate_expression(ast_node.iterator)
...@@ -211,8 +217,12 @@ class CGen: ...@@ -211,8 +217,12 @@ class CGen:
if ast_node.decl: if ast_node.decl:
self.print(f"{tkw} *{array_name} = ({tkw} *) malloc({size});") self.print(f"{tkw} *{array_name} = ({tkw} *) malloc({size});")
if self.target.is_gpu() and ast_node.array.device_flag:
self.print(f"{tkw} *d_{array_name} = ({tkw} *) pairs::device_alloc({size});")
else: else:
self.print(f"{array_name} = ({tkw} *) malloc({size});") self.print(f"{array_name} = ({tkw} *) malloc({size});")
if self.target.is_gpu() and ast_node.array.device_flag:
self.print(f"d_{array_name} = ({tkw} *) pairs::device_alloc({size});")
if isinstance(ast_node, ModuleCall): if isinstance(ast_node, ModuleCall):
module = ast_node.module module = ast_node.module
...@@ -244,6 +254,8 @@ class CGen: ...@@ -244,6 +254,8 @@ class CGen:
size = self.generate_expression(ast_node.size) size = self.generate_expression(ast_node.size)
array_name = ast_node.array.name() array_name = ast_node.array.name()
self.print(f"{array_name} = ({tkw} *) realloc({array_name}, {size});") self.print(f"{array_name} = ({tkw} *) realloc({array_name}, {size});")
if self.target.is_gpu() and ast_node.array.device_flag:
self.print(f"d_{array_name} = ({tkw} *) pairs::device_realloc(d_{array_name}, {size});")
if isinstance(ast_node, RegisterProperty): if isinstance(ast_node, RegisterProperty):
p = ast_node.property() p = ast_node.property()
......
...@@ -46,6 +46,7 @@ class Array(ASTNode): ...@@ -46,6 +46,7 @@ class Array(ASTNode):
self.arr_layout = a_layout self.arr_layout = a_layout
self.arr_ndims = len(self.arr_sizes) self.arr_ndims = len(self.arr_sizes)
self.static = False self.static = False
self.device_flag = False
for var in [s for s in self.arr_sizes if isinstance(s, Var)]: for var in [s for s in self.arr_sizes if isinstance(s, Var)]:
var.add_bonded_array(self) var.add_bonded_array(self)
......
...@@ -57,6 +57,7 @@ class Property(ASTNode): ...@@ -57,6 +57,7 @@ class Property(ASTNode):
self.prop_layout = layout self.prop_layout = layout
self.default_value = default self.default_value = default
self.volatile = volatile self.volatile = volatile
self.device_flag = False
Property.last_prop_id += 1 Property.last_prop_id += 1
def __str__(self): def __str__(self):
......
...@@ -33,6 +33,7 @@ class Var(ASTTerm): ...@@ -33,6 +33,7 @@ class Var(ASTTerm):
self.var_init_value = init_value self.var_init_value = init_value
self.mutable = True self.mutable = True
self.var_bonded_arrays = [] self.var_bonded_arrays = []
self.device_flag = False
def __str__(self): def __str__(self):
return f"Var<{self.var_name}>" return f"Var<{self.var_name}>"
......
...@@ -209,6 +209,7 @@ class Simulation: ...@@ -209,6 +209,7 @@ class Simulation:
def target(self, target): def target(self, target):
self._target = target self._target = target
self.code_gen.assign_target(target)
def generate(self): def generate(self):
assert self._target is not None, "Target not specified!" assert self._target is not None, "Target not specified!"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment