From ae45fc9b56808468ad042b0589118989ba45b9db Mon Sep 17 00:00:00 2001 From: Rafael Ravedutti <rafaelravedutti@gmail.com> Date: Thu, 5 Aug 2021 03:24:55 +0200 Subject: [PATCH] Add first (almost) working version with runtime Signed-off-by: Rafael Ravedutti <rafaelravedutti@gmail.com> --- code_gen/cgen.py | 12 +++++++++++- ir/properties.py | 7 ++++++- runtime/read_from_file.hpp | 6 +++--- sim/properties.py | 10 ++++++---- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/code_gen/cgen.py b/code_gen/cgen.py index 227f15c..d29be3a 100644 --- a/code_gen/cgen.py +++ b/code_gen/cgen.py @@ -6,6 +6,7 @@ from ir.cast import Cast from ir.bin_op import BinOp, BinOpDef from ir.data_types import Type_Int, Type_Float, Type_String, Type_Vector from ir.functions import Call +from ir.layouts import Layout_AoS, Layout_SoA, Layout_Invalid from ir.lit import Lit from ir.loops import For, Iter, ParticleFor, While from ir.math import Sqrt @@ -160,7 +161,16 @@ class CGen: "Prop_Invalid" assert ptype != "Prop_Invalid", "Invalid property type!" - self.print(f"ps->addProperty(Property({p.id()}, \"{p.name()}\", {p.name()}, {ptype}));") + + playout = "AoS" if p.layout() == Layout_AoS else \ + "SoA" if p.layout() == Layout_SoA else \ + "Invalid" + + if p.type() != Type_Vector or p.layout() == Layout_Invalid: + self.print(f"ps->addProperty(Property({p.id()}, \"{p.name()}\", {p.name()}, {ptype}));") + else: + sizes = ", ".join([str(self.generate_expression(size)) for size in ast_node.sizes()]) + self.print(f"ps->addProperty(Property({p.id()}, \"{p.name()}\", {p.name()}, {ptype}, {playout}, {sizes}));") if isinstance(ast_node, Timestep): self.generate_statement(ast_node.block) diff --git a/ir/properties.py b/ir/properties.py index 17029a6..4be233c 100644 --- a/ir/properties.py +++ b/ir/properties.py @@ -1,5 +1,6 @@ from ir.ast_node import ASTNode from ir.layouts import Layout_AoS +from ir.lit import as_lit_ast class Properties: @@ -100,13 +101,17 @@ class PropertyList(ASTNode): class RegisterProperty(ASTNode): - def __init__(self, sim, prop): + def __init__(self, sim, prop, sizes): super().__init__(sim) self.prop = prop + self.sizes_list = [as_lit_ast(sim, s) for s in sizes] self.sim.add_statement(self) def property(self): return self.prop + def sizes(self): + return self.sizes_list + def __str__(self): return f"Property<{self.prop.name()}>" diff --git a/runtime/read_from_file.hpp b/runtime/read_from_file.hpp index 85d1c2f..3e47403 100644 --- a/runtime/read_from_file.hpp +++ b/runtime/read_from_file.hpp @@ -16,7 +16,7 @@ size_t read_particle_data(PairsSim *ps, const char *filename, double *grid_buffe int read_grid_data = 0; if(in_file.is_open()) { - while(getline(in_file, line)) { + while(std::getline(in_file, line)) { std::stringstream line_stream(line); std::string in0; int i = 0; @@ -25,7 +25,6 @@ size_t read_particle_data(PairsSim *ps, const char *filename, double *grid_buffe if(!read_grid_data) { PAIRS_ASSERT(i < ps->getNumDims() * 2); grid_buffer[i] = std::stod(in0); - read_grid_data = 1; } else { PAIRS_ASSERT(i < nprops); property_t p_id = properties[i]; @@ -55,7 +54,8 @@ size_t read_particle_data(PairsSim *ps, const char *filename, double *grid_buffe i++; } - n++; + n += (read_grid_data) ? 1 : 0; + read_grid_data = 1; } in_file.close(); diff --git a/sim/properties.py b/sim/properties.py index 2313be1..79fba53 100644 --- a/sim/properties.py +++ b/sim/properties.py @@ -1,8 +1,10 @@ +from functools import reduce from ir.data_types import Type_Float, Type_Vector from ir.loops import ParticleFor from ir.memory import Malloc, Realloc from ir.properties import RegisterProperty from ir.utils import Print +import operator class PropertiesAlloc: @@ -19,15 +21,15 @@ class PropertiesAlloc: if p.type() == Type_Float: sizes = [capacity] elif p.type() == Type_Vector: - sizes = [capacity * self.sim.ndims()] + sizes = [capacity, self.sim.ndims()] else: raise Exception("Invalid property type!") if self.realloc: - Realloc(self.sim, p, sizes) + Realloc(self.sim, p, reduce(operator.mul, sizes)) else: - Malloc(self.sim, p, sizes, True) - RegisterProperty(self.sim, p) + Malloc(self.sim, p, reduce(operator.mul, sizes), True) + RegisterProperty(self.sim, p, sizes) return self.sim.block -- GitLab