Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic example without rdc requirement #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions use-cases/virtual-functions/basic-no-rdc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
KOKKOS_PATH = ${HOME}/Kokkos/kokkos
KOKKOS_DEVICES=Cuda
KOKKOS_CUDA_OPTIONS=enable_lambda,rdc
KOKKOS_ARCH = "Volta70"

#KOKKOS_DEVICES=OpenMP
#KOKKOS_CUDA_OPTIONS=enable_lambda
#KOKKOS_ARCH = "SNB"


SRC = $(wildcard $(MAKEFILE_PATH)*.cpp)
HEADERS = $(wildcard $(MAKEFILE_PATH)*.hpp)

vpath %.cpp $(sort $(dir $(SRC)))

default: build
echo "Start Build"

LINKFLAGS =
ifneq (,$(findstring Cuda,$(KOKKOS_DEVICES)))
CXX = ${KOKKOS_PATH}/bin/nvcc_wrapper
EXE = virtual.cuda
override LINKFLAGS += --remove-duplicate-link-files
else
CXX = g++
EXE = virtual.host
endif

CXXFLAGS ?= -O3 -g
override CXXFLAGS += -I./

DEPFLAGS = -M
LINK = ${CXX}

OBJ = $(notdir $(SRC:.cpp=.o))
LIB =

include $(KOKKOS_PATH)/Makefile.kokkos

build: $(EXE)

$(EXE): $(OBJ) $(KOKKOS_LINK_DEPENDS)
$(LINK) $(KOKKOS_LDFLAGS) $(LINKFLAGS) $(EXTRA_PATH) $(OBJ) $(KOKKOS_LIBS) $(LIB) -o $(EXE)

clean: kokkos-clean
rm -f *.o *.cuda *.host

# Compilation rules

%.o:%.cpp $(KOKKOS_CPP_DEPENDS) $(HEADERS)
$(CXX) $(KOKKOS_CPPFLAGS) $(KOKKOS_CXXFLAGS) $(CXXFLAGS) $(EXTRA_INC) -c $< -o $(notdir $@)
39 changes: 39 additions & 0 deletions use-cases/virtual-functions/basic-no-rdc/classes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef KOKKOS_EXAMPLE_VIRTUAL_FUNCTIONS_CLASSES_HPP
#define KOKKOS_EXAMPLE_VIRTUAL_FUNCTIONS_CLASSES_HPP

#include <Kokkos_Core.hpp>

class Foo {
protected:
int val;

public:
KOKKOS_FUNCTION
Foo();

KOKKOS_FUNCTION
virtual int value() { return 0; };

KOKKOS_FUNCTION
virtual ~Foo() {}
};

class Foo_1 : public Foo {
public:
KOKKOS_FUNCTION
Foo_1();

KOKKOS_FUNCTION
int value();
};

class Foo_2 : public Foo {
public:
KOKKOS_FUNCTION
Foo_2();

KOKKOS_FUNCTION
int value();
};

#endif // KOKKOS_EXAMPLE_VIRTUAL_FUNCTIONS_CLASSES_HPP
52 changes: 52 additions & 0 deletions use-cases/virtual-functions/basic-no-rdc/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <classes.hpp>
KOKKOS_FUNCTION
Foo::Foo() { val = 0; }

KOKKOS_FUNCTION
Foo_1::Foo_1() { val = 1; }

KOKKOS_FUNCTION
int Foo_1::value() { return val; }

KOKKOS_FUNCTION
Foo_2::Foo_2() { val = 2; }

KOKKOS_FUNCTION
int Foo_2::value() { return val; }

int main(int argc, char* argv[]) {
Kokkos::initialize(argc, argv);

{
Foo* f_1 = (Foo*)Kokkos::kokkos_malloc(sizeof(Foo_1));
Foo* f_2 = (Foo*)Kokkos::kokkos_malloc(sizeof(Foo_2));

Kokkos::parallel_for(
"CreateObjects", 1, KOKKOS_LAMBDA(const int&) {
new ((Foo_1*)f_1) Foo_1();
new ((Foo_2*)f_2) Foo_2();
});

int value_1, value_2;
Kokkos::parallel_reduce(
"CheckValues", 1,
KOKKOS_LAMBDA(const int&, int& lsum) { lsum = f_1->value(); }, value_1);

Kokkos::parallel_reduce(
"CheckValues", 1,
KOKKOS_LAMBDA(const int&, int& lsum) { lsum = f_2->value(); }, value_2);

printf("Values: %i %i\n", value_1, value_2);

Kokkos::parallel_for(
"DestroyObjects", 1, KOKKOS_LAMBDA(const int&) {
f_1->~Foo();
f_2->~Foo();
});

Kokkos::kokkos_free(f_1);
Kokkos::kokkos_free(f_2);
}

Kokkos::finalize();
}