Commit 0a5364b0 authored by Jussi Enkovaara's avatar Jussi Enkovaara
Browse files

Making monte_carlo_methods standalone project

parent 298c0529
......@@ -11,7 +11,7 @@
# CMake project
cmake_minimum_required(VERSION 2.8.10 FATAL_ERROR)
project("7_montecarlo")
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/common.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/common.cmake)
# ==================================================================================================
......
Copyright 2015
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
# README - Monte Carlo Methods
# Folders:
1. integral_basic
1. pi
1. prng
1. prime
## Integral Basic
### Files:
1. integral1d.c
1. integral1d_mpi.c
1. integral1d_OMP.c
#### integral1d.c
* This example computes integral of ( 1 / (1 + x * x) ) in x=[0,1] by using importance sampling Monte Carlo method
* This code is a serial algorithm, no need for any external library
* This is one of the code sample from the PRACE CodeVault. You can find more code samples available for download from the PRACE CodeVault here: https://gitlab.com/PRACE-4IP/CodeVault
* **Relase Date:** 28 January 2016
* **Version:** 1.0 (initial version)
* **Contributors:** Ahmet Tuncer DURAK (UHeM, ITU, TURKEY), Samet DEMIR (ITU, TURKEY)
* **Copyright:** None. The code is free to copy, modify and use, but comes with no warranty and therefore usage is at your own risk.
* **Language:** This code sample file is written in C. No external libraries are required.
* **Parallelisation Implementation:** This file is a serial code
* **Level of the code sample complexity:** Code sample complexity is simple and intended for new starters.
* **Instructions on how to compile the code:** Load a supported programming environment or compiler [GNU, PGI, Intel, Clang], Use the CodeVault CMake infrastructure, see main README.md
* **Instructions on how to run the code:** ./7_montecarlo_integral1d_serial trial#
* **Sample input:** There is one already in the code (1 / (1 + x * x)), can be changed from the code.
* **Sample output:** For a trial of 100 output is
computed value of the integral= 0.784455
true value of the integral= 0.785398
error= 0.000943
#### integral1d_mpi.c
* This code sample computes integral of ( 1 / (1 + x * x) ) in x=[0,1] by using importance sampling Monte Carlo method
* A MPI framework is needed to run the code
* This is one of the code sample from the PRACE CodeVault. You can find more code samples available for download from the PRACE CodeVault here: https://gitlab.com/PRACE-4IP/CodeVault
* **Relase Date:** 28 January 2016
* **Version: 1.0** (initial version)
* **Contributors:** Ahmet Tuncer DURAK (UHeM, ITU, TURKEY), Samet DEMIR (ITU, TURKEY)
* **Copyright:** None. The code is free to copy, modify and use, but comes with no warranty and therefore usage is at your own risk.
* **Language:** This code sample file is written in C.
* **Parallelisation Implementation:** MPI
* **Level of the code sample complexity:** Code sample complexity is simple and intended for new starters.
* **Instructions on how to compile the code:** Load a supported programming environment or compiler [GNU, PGI, Intel, Clang], Use the CodeVault CMake infrastructure, see main README.md
* **Instructions on how to run the code:** mpirun -n 4 ./7_montecarlo_integral1d_serial #trial (for 4 process)
* **Sample input:** mpirun -n 4 ./7_montecarlo_integral1d_serial 1000
* **Sample output:** For a trial of 100 output is
computed value of the integral= 0.784455
true value of the integral= 0.785398
error= 0.000943
#### integral1d_OMP.c
* This code sample computes integral of ( 1 / (1 + x * x) ) in x=[0,1] by using importance sampling Monte Carlo method
* Additional pre-requisites: OpenMP
* **Relase Date:** 28 January 2016
* **Version:** 1.0 (initial version)
* **Contributors:** Ahmet Tuncer DURAK (UHeM, ITU, TURKEY), Samet DEMIR (ITU, TURKEY)
* **Copyright:** None. The code is free to copy, modify and use, but comes with no warranty and therefore usage is at your own risk.
* **Language:** This code sample file is written in C.
* **Parallelisation Implementation:** OpenMP
* **Level of the code sample complexity:** Code sample complexity is simple and intended for new starters.
* **Instructions on how to compile the code:** Load a supported programming environment or compiler [GNU, PGI, Intel, Clang], Use the CodeVault CMake infrastructure, see main README.md
* **Instructions on how to run the code:** ./7_montecarlo_integral1d_openmp thread# trial#
* **Sample input:** ./7_montecarlo_integral1d_openmp 4 10000
* **Sample output:** For a trial of 100 output is
computed value of the integral= 0.784455
true value of the integral= 0.785398
error= 0.000943
## Pi
### Files
1. pi_mpi.c
1. pi_omp.c
1. pi_serial.c
1. pi_shared.c
1. pi_shared.h
#### pi_serial.c
* This code sample calculates the value of pi by using Monte Carlo Methods.
* This code is a serial algorithm, no need for any external library
* This is one of the code sample from the PRACE CodeVault. You can find more code samples available for download from the PRACE CodeVault here: https://gitlab.com/PRACE-4IP/CodeVault
* **Relase Date:** 28 January 2016
* **Version:** 1.0 (initial version)
* **Contributors:** Ahmet Tuncer DURAK (UHeM, ITU, TURKEY), Samet DEMIR ( ITU, TURKEY)
* **Copyright:** None. The code is free to copy, modify and use, but comes with no warranty and therefore usage is at your own risk.
* **Language:** This code sample file is written in C. No external libraries are required.
* **Parallelisation Implementation:** This file is a serial code
* **Level of the code sample complexity:** Code sample complexity is simple and intended for new starters.
* **Instructions on how to compile the code:** Load a supported programming environment or compiler [GNU, PGI, Intel, Clang], Use the CodeVault CMake infrastructure, see main README.md
* **Instructions on how to run the code:** ./7_montecarlo_pi_serial seed# N
seed (integer): seed for the pseudo random number generator
a negative value indicates current timestamp
N (integer) : number of random samples to use
* **Sample input:** ./7_montecarlo_pi_serial 10 100000
* **Sample output:** 10 100000 1 3.142156 0.000563 (seed N thread calculated_pi error)
#### pi_omp.c
* This code sample calculates the value of pi by using Monte Carlo Methods.
* **Additional pre-requisites:** OpenMP
* This is one of the code sample from the PRACE CodeVault. You can find more code samples available for download from the PRACE CodeVault here: https://gitlab.com/PRACE-4IP/CodeVault
* **Relase Date:** 28 January 2016
* **Version:** 1.0 (initial version)
* **Contributors:** Ahmet Tuncer DURAK (UHeM, ITU, TURKEY), Samet DEMIR ( ITU, TURKEY)
* **Copyright:** None. The code is free to copy, modify and use, but comes with no warranty and therefore usage is at your own risk.
* **Language:** This code sample file is written in C. No external libraries are required.
* **Parallelisation Implementation:** OpenMP
* **Level of the code sample complexity:** Code sample complexity is simple and intended for new starters.
* **Instructions on how to compile the code:** Load a supported programming environment or compiler [GNU, PGI, Intel, Clang], Use the CodeVault CMake infrastructure, see main README.md
* **Instructions on how to run the code:** ./7_montecarlo_pi_omp seed# N
seed (integer): seed for the pseudo random number generator
a negative value indicates current timestamp
N (integer) : number of random samples to use
* **Sample input:** ./7_montecarlo_pi_serial 10 100000
* **Sample output:** 10 100000 1 3.142156 0.000563 (seed N thread calculated_pi error)
#### pi_mpi.c
* This code sample calculates the value of pi by using Monte Carlo Methods.
* **Additional pre-requisites:** MPI
* This is one of the code sample from the PRACE CodeVault. You can find more code samples available for download from the PRACE CodeVault here: https://gitlab.com/PRACE-4IP/CodeVault
* **Relase Date:** 28 January 2016
* **Version:** 1.0 (initial version)
* **Contributors:** Ahmet Tuncer DURAK (UHeM, ITU, TURKEY), Samet DEMIR ( ITU, TURKEY)
* **Copyright:** None. The code is free to copy, modify and use, but comes with no warranty and therefore usage is at your own risk.
* **Language:** This code sample file is written in C. No external libraries are required.
* **Parallelisation Implementation:** MPI
* **Level of the code sample complexity:** Code sample complexity is simple and intended for new starters.
* **Instructions on how to compile the code:** Load a supported programming environment or compiler [GNU, PGI, Intel, Clang], Use the CodeVault CMake infrastructure, see main README.md
* **Instructions on how to run the code:** mpirun -n 4 ./7_montecarlo_pi_omp seed# N# (for 4 process)
seed (integer): seed for the pseudo random number generator
a negative value indicates current timestamp
N (integer) : number of random samples to use
* **Sample input:** mpirun -n 4 ./7_montecarlo_pi_omp 10 10000
* **Sample output:** 10 100000 1 3.142156 0.000563 (seed N thread calculated_pi error)
# PRACE HPC Kernels
This project is part of PRACE CodeVault providing
example codes of common HPC kernels such as dense and sparse linear
algebra, spectral and N-body methods, structured and unstructured
grids, Monte Carlo methods and parallel I/O. The code samples are
published as open source and can be used both for educational purposes
and as parts of real application suites (as permitted by particular
license).
## How to contribute
Any contributions (new code samples, bug fixes, improvements etc.) are
warmly welcome. In order to contribute, please follow the standard
Gitlab workflow:
1. Fork the project into your personal space
2. Create a feature branch
3. Work on your contributions
4. Push the commit(s) to your fork
5. Submit a merge request to the master branch
## Compilation instructions
The pre-requisites are:
* CMake 2.8.10 or higher. CMake >= 3.0.0 if you want to build everything.
* A C++ compiler
* For advanced examples, C++11/14, OpenMP support, MPI, ISPC, CUDA, OpenCL ...
In order to build the code, follow the typical CMake steps for an
out-of-source build:
```bash
mkdir build
cd build
cmake ..
make
make install # optional
```
# CorrectWindowsPaths - this module defines one macro
#
# CONVERT_CYGWIN_PATH( PATH )
# This uses the command cygpath (provided by cygwin) to convert
# unix-style paths into paths useable by cmake on windows
macro (CONVERT_CYGWIN_PATH _path)
if (WIN32)
EXECUTE_PROCESS(COMMAND cygpath.exe -m ${${_path}}
OUTPUT_VARIABLE ${_path})
string (STRIP ${${_path}} ${_path})
endif (WIN32)
endmacro (CONVERT_CYGWIN_PATH)
function(clone_source src dst)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${dst}
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${src} ${CMAKE_CURRENT_BINARY_DIR}/${dst}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${src}
COMMENT "Clone ${src} to ${dst}"
)
endfunction()
function(dummy_install target_name lib_name)
install(CODE "MESSAGE(\"${target_name} can only be built with ${lib_name}.\")")
endfunction()
macro(get_sources_and_options _sources _option_list _option_name)
set( ${_sources} )
set( ${_option_list} )
set( _found_options False)
foreach(arg ${ARGN})
if ("x${arg}" STREQUAL "x${_option_name}")
set (_found_options True)
else()
if (_found_options)
list(APPEND ${_option_list} ${arg})
else()
list(APPEND ${_sources} ${arg})
endif()
endif()
endforeach()
endmacro()
# MACRO picks correct compiler flags. For example
# select_compiler_flags(cxx_flags
# GNU "-std=c++11 -mnative -Wall -Werror"
# Intel "-std=c++11 -axavx,core-avx2"
# CLANG "-std=c++11 -Weverything"
# PGI "-std=c++11"
# )
macro(select_compiler_flags _flags)
set( ${_flags} )
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(_compiler "GNU")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(_compiler "CLANG")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
set(_compiler "CLANG")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
set(_compiler "Intel")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(_compiler "MSCV")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "PGI")
set(_compiler "PGI")
endif()
set (_found_compiler False)
foreach(arg ${ARGN})
if ("x${arg}" STREQUAL "x${_compiler}")
set(_found_compiler True)
else()
if (_found_compiler)
set(${_flags} ${arg})
set(_found_compiler False)
endif()
endif()
endforeach()
endmacro()
# ==================================================================================================
# This file is part of the CodeVault project. The project is licensed under Apache Version 2.0.
# CodeVault is part of the EU-project PRACE-4IP (WP7.3.C).
#
# Author(s):
# Cedric Nugteren <cedric.nugteren@surfsara.nl>
#
# ==================================================================================================
#
# Defines the following variables:
# FFTW_FOUND Boolean holding whether or not the FFTW3 library was found
# FFTW_INCLUDE_DIRS The FFTW3 include directory
# FFTW_LIBRARIES The FFTW3 library
#
# In case FFTW3 is not installed in the default directory, set the FFTW_ROOT variable to point to
# the root of FFTW3, such that 'fftw3.h' can be found in $FFTW_ROOT/include. This can either be done
# using an environmental variable (e.g. export FFTW_ROOT=/path/to/fftw3) or using a CMake variable
# (e.g. cmake -DFFTW_ROOT=/path/to/fftw3 ..).
#
# ==================================================================================================
# Sets the possible install locations
set(FFTW_HINTS
${FFTW_ROOT}
$ENV{FFTW_ROOT}
)
set(FFTW_PATHS
/usr
/usr/local
)
# Finds the include directories
find_path(FFTW_INCLUDE_DIRS
NAMES fftw3.h
HINTS ${FFTW_HINTS}
PATH_SUFFIXES include api inc include/x86_64 include/x64
PATHS ${FFTW_PATHS}
DOC "FFTW3 include header fftw3.h"
)
mark_as_advanced(FFTW_INCLUDE_DIRS)
# Finds the library
find_library(FFTW_LIBRARIES
NAMES fftw3
HINTS ${FFTW_HINTS}
PATH_SUFFIXES lib lib64 lib/x86_64 lib/x64 lib/x86 lib/Win32
PATHS ${FFTW_PATHS}
DOC "FFTW3 library"
)
mark_as_advanced(FFTW_LIBRARIES)
# ==================================================================================================
# Notification messages
if(NOT FFTW_INCLUDE_DIRS)
message(STATUS "Could NOT find 'fftw3.h', install FFTW3 or set FFTW_ROOT")
endif()
if(NOT FFTW_LIBRARIES)
message(STATUS "Could NOT find the FFTW3 library, install it or set FFTW_ROOT")
endif()
# Determines whether or not FFTW3 was found
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(FFTW DEFAULT_MSG FFTW_INCLUDE_DIRS FFTW_LIBRARIES)
# ==================================================================================================
find_package(Common)
SET(ISPC_VERSION_REQUIRED "1.8.2")
IF (NOT ISPC_EXECUTABLE)
# if ($ENV{ISPC})
# FIND_PROGRAM(ISPC_EXECUTABLE NAME ispc PATH $ENV{ISPC} DOC "Path to the ISPC executable.")
# endif()
FIND_PROGRAM(ISPC_EXECUTABLE ispc DOC "Path to the ISPC executable.")
IF (NOT ISPC_EXECUTABLE)
MESSAGE(STATUS "Intel SPMD Compiler (ISPC) is not found.")
ELSE()
MESSAGE(STATUS "Found Intel SPMD Compiler (ISPC): ${ISPC_EXECUTABLE}")
set(ISPC_FOUND True)
ENDIF()
ENDIF()
if (NOT ISPC_EXECUTABLE)
set(ISPC_FOUND False)
else(NOT ISPC_EXECUTABLE)
set(ISPC_FOUND True)
IF(NOT ISPC_VERSION)
EXECUTE_PROCESS(COMMAND ${ISPC_EXECUTABLE} --version OUTPUT_VARIABLE ISPC_OUTPUT)
STRING(REGEX MATCH " ([0-9]+[.][0-9]+[.][0-9]+)(dev|knl|ptx)? " DUMMY "${ISPC_OUTPUT}")
SET(ISPC_VERSION ${CMAKE_MATCH_1})
#IF (ISPC_VERSION VERSION_LESS ISPC_VERSION_REQUIRED)
# MESSAGE(FATAL_ERROR "Need at least version ${ISPC_VERSION_REQUIRED} of Intel SPMD Compiler (ISPC).")
# ENDIF()
MESSAGE(STATUS "Found ISPC version ${ISPC_VERSION}")
SET(ISPC_VERSION ${ISPC_VERSION} CACHE STRING "ISPC Version")
MARK_AS_ADVANCED(ISPC_VERSION)
MARK_AS_ADVANCED(ISPC_EXECUTABLE)
ENDIF()
function(ispc_compile _obj_list)
get_sources_and_options(sources compile_flags "COMPILE_FLAGS" ${ARGN})
set(__XEON__ True)
IF (__XEON__)
SET (ISPC_TARGET_EXT ${CMAKE_CXX_OUTPUT_EXTENSION})
ELSE()
SET (ISPC_TARGET_EXT .cpp)
SET (ISPC_ADDITIONAL_ARGS ${ISPC_ADDITIONAL_ARGS} --opt=force-aligned-memory)
ENDIF()
IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
SET(ISPC_ARCHITECTURE "x86-64")
ELSE()
SET(ISPC_ARCHITECTURE "x86")
ENDIF()
SET(ISPC_TARGET_DIR ${CMAKE_CURRENT_BINARY_DIR})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} ${ISPC_TARGET_DIR})
IF(ISPC_INCLUDE_DIR)
STRING(REPLACE ";" ";-I;" ISPC_INCLUDE_DIR_PARMS "${ISPC_INCLUDE_DIR}")
SET(ISPC_INCLUDE_DIR_PARMS "-I" ${ISPC_INCLUDE_DIR_PARMS})
ENDIF()
set(___XEON___ True)
IF (__XEON__)
STRING(REPLACE ";" "," ISPC_TARGET_ARGS "${ISPC_TARGETS}")
ELSE()
# SET(ISPC_TARGET_ARGS generic-16)
#SET(ISPC_ADDITIONAL_ARGS ${ISPC_ADDITIONAL_ARGS} --emit-c++ -D__XEON_PHI__ --c++-include-file=${ISPC_DIR}/examples/intrinsics/knc.h)j
ENDIF()
SET(ISPC_OBJECTS ${${_obj_list}})
foreach (src ${sources})
get_filename_component(fname ${src} NAME_WE)
get_filename_component(dir ${src} PATH)
IF("${dir}" STREQUAL "")
SET(outdir ${ISPC_TARGET_DIR})
ELSE("${dir}" STREQUAL "")
SET(outdir ${ISPC_TARGET_DIR}/${dir})
ENDIF("${dir}" STREQUAL "")
SET(outdirh ${ISPC_TARGET_DIR})
SET(deps "")
IF (EXISTS ${outdir}/${fname}.dev.idep)
FILE(READ ${outdir}/${fname}.dev.idep contents)
STRING(REPLACE " " ";" contents "${contents}")
STRING(REPLACE ";" "\\\\;" contents "${contents}")
STRING(REPLACE "\n" ";" contents "${contents}")
FOREACH(dep ${contents})
IF (EXISTS ${dep})
SET(deps ${deps} ${dep})
ENDIF (EXISTS ${dep})
ENDFOREACH(dep ${contents})
ENDIF ()
SET(results "${outdir}/${fname}.dev${ISPC_TARGET_EXT}")
# if we have multiple targets add additional object files
IF (__XEON__)
LIST(LENGTH ISPC_TARGETS NUM_TARGETS)
IF (NUM_TARGETS GREATER 1)
FOREACH(target ${ISPC_TARGETS})
SET(results ${results} "${outdir}/${fname}.dev_${target}${ISPC_TARGET_EXT}")
ENDFOREACH()
ENDIF()
ENDIF()
IF (WIN32)
SET(ISPC_ADDITIONAL_ARGS ${ISPC_ADDITIONAL_ARGS} --dllexport)
ELSE()
SET(ISPC_ADDITIONAL_ARGS ${ISPC_ADDITIONAL_ARGS} --pic)
ENDIF()
separate_arguments(compile_flags)
ADD_CUSTOM_COMMAND(
OUTPUT ${results} ${outdirh}/${fname}_ispc.h
COMMAND ${CMAKE_COMMAND} -E make_directory ${outdir}
COMMAND ${ISPC_EXECUTABLE}
${compile_flags}
-I ${CMAKE_CURRENT_SOURCE_DIR}
${ISPC_INCLUDE_DIR_PARMS}
--arch=${ISPC_ARCHITECTURE}
# --addressing=${EMBREE_ISPC_ADDRESSING}
# -O3
#--target=${ISPC_TARGET_ARGS}
#--woff
# --opt=fast-math
${ISPC_ADDITIONAL_ARGS}
-h ${outdirh}/${fname}_ispc.h
-MMM ${outdir}/${fname}.dev.idep
-o ${outdir}/${fname}.dev${ISPC_TARGET_EXT}
${CMAKE_CURRENT_SOURCE_DIR}/${src}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${src} ${deps}
COMMENT "Building ISPC object ${outdir}/${fname}.dev${ISPC_TARGET_EXT}"
)
SET(ISPC_OBJECTS ${ISPC_OBJECTS} ${results})
endforeach()
set(${_obj_list} ${ISPC_OBJECTS} PARENT_SCOPE)
endfunction()
endif(NOT ISPC_EXECUTABLE)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/Languages")
enable_language(ICPC OPTIONAL)
enable_language(IFORT OPTIONAL)
find_package(Common)
set(ICPC_FOUND True)
if (${CMAKE_ICPC_COMPILER} STREQUAL "CMAKE_ICPC_COMPILER-NOTFOUND")
set(ICPC_FOUND False)
else()
function(icpc_sources)
get_sources_and_options(sources compile_flags "COMPILE_FLAGS" ${ARGN})
set_source_files_properties(${sources} PROPERTIES LANGUAGE ICPC)
foreach (file ${sources})
set_property(SOURCE ${file} APPEND_STRING PROPERTY COMPILE_FLAGS " -I${CMAKE_CURRENT_SOURCE_DIR}/${CMAKE_CXX_FLAGS} ${compile_flags} ")
endforeach()
endfunction()
function(icpc_add_executable icpc_target)
get_sources_and_options(sources link_flags "LINK_FLAGS" ${ARGN})
add_executable(${icpc_target} ${sources})
set_target_properties(${icpc_target} PROPERTIES LINKER_LANGUAGE ICPC)
set_property(TARGET ${icpc_target} APPEND_STRING PROPERTY LINK_FLAGS " ${link_flags} ")
endfunction()
endif()
set(IFORT_FOUND True)
if (${CMAKE_IFORT_COMPILER} STREQUAL "CMAKE_IFORT_COMPILER-NOTFOUND")
set(IFORT_FOUND False)
else()
#function(ifort_sources)
#set_source_files_properties(${ARGN} PROPERTIES LANGUAGE IFORT)
#endfunction()