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

Add cmake example

parent a7ddcc4c
No related branches found
No related tags found
No related merge requests found
! 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
#include <stdio.h>
#include <time.h>
void saxpy(int n, double a, double* x, double* y, double* z);
double maxValue(double myArray[], int size);
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));
}
! 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
#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;
}
cmake_minimum_required (VERSION 2.8.11)
project (SAXPY)
# 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)
#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;
}
void saxpy(int n, double a, double* x, double* y, double* z);
double maxValue(double myArray[], int size);
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