# SPDX-FileCopyrightText: Copyright (c) 2017 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: LicenseRef-NvidiaProprietary
#
# NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
# property and proprietary rights in and to this material, related
# documentation and any modifications thereto. Any use, reproduction,
# disclosure or distribution of this material and related documentation
# without an express license agreement from NVIDIA CORPORATION or
# its affiliates is strictly prohibited.

TEST = tsort3
FC ?= nvfortran
CXX = nvcc
EXE = exe

CUFILE = keys.cu
# nvcc requires specifying compute capability to generate code for. The following logic
# attempts to figure this out automatically by looking through GPUs on current systems.
# However, it if fails to do so it may need done manually like the example.
# Example for Ampere: --generate-code arch=compute_80,code=sm_80
SYSCOMPCAP := $(shell nvaccelinfo | grep "Default Target:" | awk -F "cc" '{ print $$2 }' | sort | uniq)
NVCCOPTIONS ?= $(foreach CCAP,$(SYSCOMPCAP), --generate-code arch=compute_$(CCAP),code=sm_$(CCAP))

# These are compatible flags between NVHPC and nvcc
CXXFLAGS ?= -c $(NVCCOPTIONS)
ifeq ($(TEST),tsort3)
FCFLAGS ?= -acc=gpu -gpu=nordc -c++libs -cuda -cudalib=curand
else
FCFLAGS ?= -acc=gpu -gpu=nordc,managed -c++libs -cuda -cudalib=curand
endif

all: build run verify

build: $(TEST).f90
	$(CXX) $(CXXFLAGS) $(CUFILE) -o keys.o
	$(FC) $(FCFLAGS) keys.o -o $(TEST).$(EXE) $<

run: $(TEST).$(EXE)
	$(RUN) ./$(TEST).$(EXE)

verify:

clean:
	@echo 'Cleaning up...'
	@rm -rf *.$(EXE) *.dwf *.pdb *.mod prof *.o
