Skip to content
Snippets Groups Projects
Commit 6899f530 authored by Mathieu Peybernes's avatar Mathieu Peybernes
Browse files

Advanced cmake

parent 5de9f066
No related branches found
No related tags found
No related merge requests found
cmake_minimum_required (VERSION 2.8.11)
project (SAXPY)
# Make sure the compiler can find include files
include_directories(${PROJECT_SOURCE_DIR})
# Create a library called "saxpy" which includes the source file "saxpy.c".
# The extension is already found. Any number of sources could be listed here.
add_library (saxpy saxpy.c)
# Make sure the compiler can find include files for our saxpy library
# when other libraries or executables link to Hello
target_include_directories (saxpy PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
# Create executable called "exec" that is built from the source file
# "main.c". The extensions are automatically found.
add_executable (exec main.c)
# Link the executable to the saxpy library. Since the saxpy library has
# public include directories we will use those link directories when building
# exec
target_link_libraries (exec LINK_PUBLIC saxpy)
# Create executable called "exec" that is built from the source files
# "main.c" and "saxpy.c". The extensions are automatically found.
add_executable (exec main.c saxpy.c)
cmake_minimum_required (VERSION 2.8.11)
project (SAXPY)
set(CMAKE_VERBOSE_MAKEFILE ON)
# Since different compilers support different options, a typical use of this command is in a compiler-specific conditional clause:
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# using Clang
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wall -Wextra -pedantic -Werror -O3)
# using GCC
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
# using Intel C++
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# using Visual Studio C++
endif()
# List of source files into "SOURCES"
file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/src/*.c")
# Create a library called "saxpy" which includes the source file "saxpy.c".
# The extension is already found. Any number of sources could be listed here.
add_library (saxpy ${SOURCES})
# Make sure the compiler can find include files for our saxpy library
# when other libraries or executables link to Hello
target_include_directories (saxpy PUBLIC ${PROJECT_SOURCE_DIR}/include)
# Create executable called "exec" that is built from the source file
# "main.c". The extensions are automatically found.
add_executable (exec main.c)
# Link the executable to the saxpy library. Since the saxpy library has
# public include directories we will use those link directories when building
# exec
target_link_libraries (exec LINK_PUBLIC saxpy)
void saxpy(int n, double a, double* x, double* y, double* z);
double maxValue(double myArray[], int size);
#include <stdio.h>
#include <time.h>
#include <saxpy.h>
int main() {
int i;
int n=100000000;
double a=2.0;
double x[n];
double y[n];
double z[n];
clock_t start_time, end_time;
/* Init */
for (i = 0; i < n; i++){
z[i] = 0.0;
x[i] = 1.0;
y[i] = 2.0;
}
start_time = clock();
/* Call saxpy */
saxpy(n,a,x,y,z);
end_time = clock();
printf ("timing for saxpy C: %f ms\n",((double)(end_time - start_time)/CLOCKS_PER_SEC)*1000.);
printf ("max value of z: %f\n",maxValue(z,n));
}
#include <stdio.h>
void saxpy(int n, double a, double* x, double* y, double* z) {
for(int i = 0; i < n; i++)
{
z[i] = a*x[i] + y[i];
}
}
double maxValue(double myArray[], int size) {
int i;
double maxValue = myArray[0];
for (i = 1; i < size; ++i) {
if ( myArray[i] > maxValue ) {
maxValue = myArray[i];
}
}
return maxValue;
}
! Main program
program main
use iso_fortran_env, only: di=>int64, dp=>real64
use saxpy_mod
integer nmax
parameter (nmax=100000000)
real(dp) x(nmax), y(nmax), z(nmax)
real(dp) rate
integer(di) :: startc, endc
z=0._dp
x=1._dp
y=2._dp
call system_clock(count_rate=rate)
call system_clock(startc)
call saxpy(nmax, 2.0_dp, x, y, z)
call system_clock(endc)
print*, "L2 norm of z = ",norm2(z)
write(*,*) "timing for saxpy: ", real(endc-startc, dp)/rate*1000, "ms"
end program main
! saxpy routine
module saxpy_mod
use iso_fortran_env, only: di=>int64, dp=>real64
contains
subroutine saxpy(n, a, x, y, z)
integer, intent(in) :: n
real(dp), intent(in) :: x(:), y(:), a
real(dp), intent(out) :: z(:)
integer :: i
do i=1,n
z(i) = a*x(i)+y(i)
enddo
end subroutine saxpy
end module saxpy_mod
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment