# 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 = tsortcuf1
FC ?= nvfortran
CXX = nvcc
EXE = exe

ifeq ($(TEST),tsortcuf1)
CUFILE = csort.cu
else
CUFILE = key.cu
endif

# These are compatible flags between NVHPC and nvcc
FCFLAGS ?= -gpu=nordc -c++libs
# 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))
CXXFLAGS ?= -c $(NVCCOPTIONS)

all: build run verify

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

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

verify:

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