Skip to content

Commit

Permalink
Fixed pypackage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wesley Kwiecinski committed Jul 18, 2024
1 parent c47edf4 commit 0c37e47
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 104 deletions.
3 changes: 0 additions & 3 deletions tools/milhoja_pypkg/src/milhoja/DataPacketGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def __init__(
self, tf_spec: TaskFunction, indent: int, logger: BasicLogger,
sizes: dict
):
if not isinstance(tf_spec, TaskFunction):
raise TypeError("TF Spec was not derived from task function.")

self._TOOL_NAME = "Milhoja DataPacket"
self._sizes = deepcopy(sizes)
self._indent = indent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
from . import LOG_LEVEL_BASIC


class DataPacketC2FModuleGenerator(AbcCodeGenerator):
class DataPacketModGenerator(AbcCodeGenerator):
"""
Responsible for generating the module interface file for use by the
fortran task function and interoperability layers. Nothing is generated
for C++ based task functions.
"""

# C2F Module generator uses its own specific type mapping for the
# fortran interface.
_TYPE_MAPPING = {
Expand Down
8 changes: 5 additions & 3 deletions tools/milhoja_pypkg/src/milhoja/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@
from .TaskFunctionC2FGenerator_cpu_F import TaskFunctionC2FGenerator_cpu_F
from .TaskFunctionGenerator_OpenACC_F import TaskFunctionGenerator_OpenACC_F
from .TaskFunctionGenerator_cpu_F import TaskFunctionGenerator_cpu_F
from .TaskFunctionC2FGenerator_OpenACC_F import TaskFunctionC2FGenerator_OpenACC_F
from .TaskFunctionCpp2CGenerator_OpenACC_F import TaskFunctionCpp2CGenerator_OpenACC_F
from .DataPacketModuleGenerator import DataPacketModuleGenerator
from .TaskFunctionC2FGenerator_OpenACC_F \
import TaskFunctionC2FGenerator_OpenACC_F
from .TaskFunctionCpp2CGenerator_OpenACC_F \
import TaskFunctionCpp2CGenerator_OpenACC_F
from .DataPacketModGenerator import DataPacketModGenerator

# Functions that use classes
from .generate_data_item import generate_data_item
Expand Down
12 changes: 7 additions & 5 deletions tools/milhoja_pypkg/src/milhoja/generate_task_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from . import TileWrapperModGenerator
from . import TaskFunctionC2FGenerator_OpenACC_F
from . import TaskFunctionCpp2CGenerator_OpenACC_F
from . import DataPacketModuleGenerator
from . import DataPacketModGenerator


def generate_task_function(tf_spec, destination, overwrite, indent, logger):
Expand Down Expand Up @@ -64,16 +64,18 @@ def generate_task_function(tf_spec, destination, overwrite, indent, logger):
generator.generate_source_code(destination, overwrite)
assert destination.joinpath(generator.source_filename).is_file()

generator = TaskFunctionC2FGenerator_OpenACC_F(tf_spec, indent, logger)
generator = \
TaskFunctionC2FGenerator_OpenACC_F(tf_spec, indent, logger)
generator.generate_source_code(destination, overwrite)
assert destination.joinpath(generator.source_filename).is_file()

generator = TaskFunctionCpp2CGenerator_OpenACC_F(tf_spec, indent, logger)
generator = \
TaskFunctionCpp2CGenerator_OpenACC_F(tf_spec, indent, logger)
generator.generate_source_code(destination, overwrite)
assert destination.joinpath(generator.source_filename).is_file()

generator = DataPacketModuleGenerator(tf_spec, indent, logger)
generator.generate_source_code(destination, indent, logger)
generator = DataPacketModGenerator(tf_spec, indent, logger)
generator.generate_source_code(destination, indent)
assert destination.joinpath(generator.source_filename).is_file()

elif (language.lower() in ["c++", "fortran"]) and \
Expand Down
144 changes: 53 additions & 91 deletions tools/milhoja_pypkg/src/milhoja/tests/TestDataPacketGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
BasicLogger,
TaskFunction
)
from milhoja.Cpp2CLayerGenerator import Cpp2CLayerGenerator
from milhoja.C2FortranLayerGenerator import C2FortranLayerGenerator
from milhoja.TaskFunctionCpp2CGenerator_OpenACC_F \
import TaskFunctionCpp2CGenerator_OpenACC_F
from milhoja.TaskFunctionC2FGenerator_OpenACC_F \
import TaskFunctionC2FGenerator_OpenACC_F
from milhoja.FortranTemplateUtility import FortranTemplateUtility
from milhoja.TemplateUtility import TemplateUtility
from milhoja.DataPacketC2FModuleGenerator import DataPacketC2FModuleGenerator
from milhoja.DataPacketModGenerator import DataPacketModGenerator

_FILE_PATH = Path(__file__).resolve().parent
_DATA_PATH = _FILE_PATH.joinpath("data")
Expand Down Expand Up @@ -238,10 +240,8 @@ def testPacketGeneration(self):
destination, overwrite=False
)

generated_name_cpp = Path(
destination,
generator.source_filename
)
generated_name_cpp = \
Path(destination, generator.source_filename)
correct_name_cpp = json_path.stem.replace("REF_", "")
correct_name_cpp = Path(
_DATA_PATH,
Expand All @@ -257,10 +257,8 @@ def testPacketGeneration(self):
json_path, generated_cpp, correct
)

generated_name_h = Path(
destination,
generator.header_filename
)
generated_name_h = \
Path(destination, generator.header_filename)
correct_name_h = json_path.stem.replace("REF_", "")
correct_name_h = Path(
_DATA_PATH,
Expand All @@ -275,77 +273,10 @@ def testPacketGeneration(self):
json_path, generated_h, correct
)

# ..todo::
# * currently the cpp2c layer is only generated when
# using a fortran task function there should be
# another "cpp2c layer" that's just for cpp task
# functions.
generated_cpp2c = None
generated_c2f = None
generated_dp_mod = None
if generator.language == "fortran":
# check cpp2c layer.
generated_cpp2c = Path(
destination,
generator.cpp2c_layer.source_filename
)
correct_cpp2c = json_path.stem.replace("REF_", "")
correct_cpp2c = Path(
_DATA_PATH,
test[self.FOLDER],
"REF_" + str(correct_cpp2c) + "_Cpp2C.cpp"
)
with open(generated_cpp2c, 'r') as generated:
with open(correct_cpp2c, 'r') as correct:
self.check_generated_files(
json_path, generated, correct
)

# check c2f layer.
generated_c2f = Path(
destination,
generator.c2f_layer.source_filename
)
correct_c2f = json_path.stem.replace("REF_", "")
correct_c2f = Path(
_DATA_PATH,
test[self.FOLDER],
"REF_" + str(correct_c2f) + "_C2F.F90"
)
with open(generated_c2f, 'r') as generated:
with open(correct_c2f, 'r') as correct:
self.check_generated_files(
json_path, generated, correct
)

# check module file
generated_dp_mod = Path(
destination,
generator.dp_module.source_filename
)
correct_dp_mod = json_path.stem.replace("REF_", "")
correct_dp_mod = Path(
_DATA_PATH,
test[self.FOLDER],
"REF_DataPacket_" + str(correct_dp_mod) +
"_c2f_mod.F90"
)
with open(generated_dp_mod, 'r') as generated:
with open(correct_dp_mod, 'r') as correct:
self.check_generated_files(
json_path, generated, correct
)

# clean up generated files if test passes.
try:
os.remove(generated_name_cpp)
os.remove(generated_name_h)
if generated_cpp2c:
os.remove(generated_cpp2c)
if generated_c2f:
os.remove(generated_c2f)
if generated_dp_mod:
os.remove(generated_dp_mod)
# be careful when cleaning up here
for file in Path(destination).glob("cg-tpl.*.cpp"):
os.remove(file)
Expand Down Expand Up @@ -479,7 +410,7 @@ def testCpp2CGenerator(self):
# use default logging value for now
logger = BasicLogger(LOG_LEVEL_NONE)
destination = Path.cwd()
cpp2c = Cpp2CLayerGenerator(tf_spec, 4, logger)
cpp2c = TaskFunctionCpp2CGenerator_OpenACC_F(tf_spec, 4, logger)

with self.assertRaises(
LogicError,
Expand All @@ -494,7 +425,20 @@ def testCpp2CGenerator(self):
):
cpp2c.generate_source_code(destination, overwrite=False)

generated_cpp2c = Path(destination, cpp2c.source_filename)
correct_cpp2c = json_path.stem.replace("REF_", "")
correct_cpp2c = Path(
_DATA_PATH, test[self.FOLDER],
"REF_" + str(correct_cpp2c) + "_Cpp2C.cpp"
)
with open(generated_cpp2c, 'r') as generated:
with open(correct_cpp2c, 'r') as correct:
self.check_generated_files(
json_path, generated, correct
)

# cleanup
os.remove(generated_cpp2c)
try:
for file in Path(destination).glob("cg-tpl.*.cpp"):
os.remove(file)
Expand All @@ -516,28 +460,38 @@ def testModGeneration(self):

if tf_spec.language.lower() == "c++":
with self.assertRaises(LogicError, msg="Wrong language"):
mod_generator = DataPacketC2FModuleGenerator(
mod_generator = DataPacketModGenerator(
tf_spec, 4, logger
)
continue

mod_generator = DataPacketC2FModuleGenerator(
tf_spec, 4, logger
)

mod_generator = DataPacketModGenerator(tf_spec, 4, logger)
with self.assertRaises(LogicError, msg="Header gen should fail."):
mod_generator.generate_header_code(destination, True)

mod_generator.generate_source_code(destination, True)

with self.assertRaises(
FileExistsError,
msg="File was overwritten!"
):
mod_generator.generate_source_code(destination, False)

generated_dp_mod = \
Path(destination, mod_generator.source_filename)
correct_dp_mod = json_path.stem.replace("REF_", "")
correct_dp_mod = Path(
_DATA_PATH, test[self.FOLDER],
"REF_DataPacket_" + str(correct_dp_mod) + "_c2f_mod.F90"
)
with open(generated_dp_mod, 'r') as generated:
with open(correct_dp_mod, 'r') as correct:
self.check_generated_files(
json_path, generated, correct
)

os.remove(Path(destination, mod_generator.source_filename))

def testC2fGenerator(self):
def testC2FGenerator(self):
for test in self._sedov:
json_path = test[self.JSON]
sizes = test[self.SIZES]
Expand All @@ -552,24 +506,32 @@ def testC2fGenerator(self):
logger = BasicLogger(LOG_LEVEL_NONE)
destination = Path.cwd()

c2f = C2FortranLayerGenerator(
tf_spec, 4, logger
)

c2f = TaskFunctionC2FGenerator_OpenACC_F(tf_spec, 4, logger)
with self.assertRaises(
LogicError,
msg="C2F generate_header was not called?"
):
c2f.generate_header_code(destination, overwrite=True)

c2f.generate_source_code(destination, overwrite=True)

with self.assertRaises(
FileExistsError,
msg="C2F file was overwritten!"
):
c2f.generate_source_code(destination, overwrite=False)

generated_c2f = Path(destination, c2f.source_filename)
correct_c2f = json_path.stem.replace("REF_", "")
correct_c2f = Path(
_DATA_PATH, test[self.FOLDER],
"REF_" + str(correct_c2f) + "_C2F.F90"
)
with open(generated_c2f, 'r') as generated:
with open(correct_c2f, 'r') as correct:
self.check_generated_files(
json_path, generated, correct
)

try:
for file in Path(destination).glob("*.F90"):
os.remove(file)
Expand Down

0 comments on commit 0c37e47

Please sign in to comment.