diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..59911a1 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +project(phys-743-examples) +cmake_minimum_required(VERSION 3.0.0) + +add_compile_options(-Wall -Wextra -pedantic) + +add_subdirectory(optimization) +add_subdirectory(openmp) +add_subdirectory(mpi) diff --git a/compilaton/Makefile b/compilaton/Makefile new file mode 100644 index 0000000..daf0ef3 --- /dev/null +++ b/compilaton/Makefile @@ -0,0 +1,37 @@ +CC = gcc +CFLAGS = -O3 -ftree-vectorize +LDFLAGS = -lm + +all: code + +# This make file cut the compilation in 3 phases only for the purpose to show the phases + +# compilation : assembly to machine code +code.o: code.s + @echo ""; echo "" + @echo " >> $(CC) -c $(CFLAGS) $< -o $@" + @$(CC) -c $(CFLAGS) $< -o $@ + @objdump -d $@ + +# compilation : code to assembly code +code.s: code.i + @echo ""; echo "" + @echo " >> $(CC) -S $(CFLAGS) $< -o $@" + @$(CC) -S $(CFLAGS) $< -o $@ + @cat $@ + +# precompilation +code.i: code.c + @echo ""; echo "" + @echo " >> $(CC) -E $(CFLAGS) $< -o $@" + @$(CC) -E $(CFLAGS) $< -o $@ + @tail -n 12 $@ + +# linking +code: code.o + @echo ""; echo "" + @echo " >> $(CC) $(LDFLAGS) code.o -o $@" + @$(CC) $(LDFLAGS) code.o -o $@ + +clean: + rm -f *.o code code.i code.s diff --git a/compilaton/code b/compilaton/code new file mode 100755 index 0000000..4fe4105 Binary files /dev/null and b/compilaton/code differ diff --git a/compilaton/code.c b/compilaton/code.c new file mode 100644 index 0000000..cfc7426 --- /dev/null +++ b/compilaton/code.c @@ -0,0 +1,17 @@ +#include +#include + +#define up 10 + +int main() { + int i, n; + n = 0; + + for (i = 0; i < up; i++){ + n = n + 1; + } + + printf("n = %i\n", n); + + return 0; +} diff --git a/compilaton/code.i b/compilaton/code.i new file mode 100644 index 0000000..4833e42 --- /dev/null +++ b/compilaton/code.i @@ -0,0 +1,1977 @@ +# 0 "code.c" +# 0 "" +# 0 "" +# 1 "/usr/include/stdc-predef.h" 1 3 4 +# 0 "" 2 +# 1 "code.c" +# 1 "/usr/include/stdio.h" 1 3 4 +# 27 "/usr/include/stdio.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 1 3 4 +# 33 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 3 4 +# 1 "/usr/include/features.h" 1 3 4 +# 461 "/usr/include/features.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 1 3 4 +# 449 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4 +# 450 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/long-double.h" 1 3 4 +# 451 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4 +# 462 "/usr/include/features.h" 2 3 4 +# 485 "/usr/include/features.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 1 3 4 +# 10 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/gnu/stubs-64.h" 1 3 4 +# 11 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 2 3 4 +# 486 "/usr/include/features.h" 2 3 4 +# 34 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 2 3 4 +# 28 "/usr/include/stdio.h" 2 3 4 + + + + + +# 1 "/usr/lib/gcc/x86_64-linux-gnu/11/include/stddef.h" 1 3 4 +# 209 "/usr/lib/gcc/x86_64-linux-gnu/11/include/stddef.h" 3 4 + +# 209 "/usr/lib/gcc/x86_64-linux-gnu/11/include/stddef.h" 3 4 +typedef long unsigned int size_t; +# 34 "/usr/include/stdio.h" 2 3 4 + + +# 1 "/usr/lib/gcc/x86_64-linux-gnu/11/include/stdarg.h" 1 3 4 +# 40 "/usr/lib/gcc/x86_64-linux-gnu/11/include/stdarg.h" 3 4 +typedef __builtin_va_list __gnuc_va_list; +# 37 "/usr/include/stdio.h" 2 3 4 + +# 1 "/usr/include/x86_64-linux-gnu/bits/types.h" 1 3 4 +# 27 "/usr/include/x86_64-linux-gnu/bits/types.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4 +# 28 "/usr/include/x86_64-linux-gnu/bits/types.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/timesize.h" 1 3 4 +# 29 "/usr/include/x86_64-linux-gnu/bits/types.h" 2 3 4 + + +typedef unsigned char __u_char; +typedef unsigned short int __u_short; +typedef unsigned int __u_int; +typedef unsigned long int __u_long; + + +typedef signed char __int8_t; +typedef unsigned char __uint8_t; +typedef signed short int __int16_t; +typedef unsigned short int __uint16_t; +typedef signed int __int32_t; +typedef unsigned int __uint32_t; + +typedef signed long int __int64_t; +typedef unsigned long int __uint64_t; + + + + + + +typedef __int8_t __int_least8_t; +typedef __uint8_t __uint_least8_t; +typedef __int16_t __int_least16_t; +typedef __uint16_t __uint_least16_t; +typedef __int32_t __int_least32_t; +typedef __uint32_t __uint_least32_t; +typedef __int64_t __int_least64_t; +typedef __uint64_t __uint_least64_t; + + + +typedef long int __quad_t; +typedef unsigned long int __u_quad_t; + + + + + + + +typedef long int __intmax_t; +typedef unsigned long int __uintmax_t; +# 141 "/usr/include/x86_64-linux-gnu/bits/types.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/typesizes.h" 1 3 4 +# 142 "/usr/include/x86_64-linux-gnu/bits/types.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/time64.h" 1 3 4 +# 143 "/usr/include/x86_64-linux-gnu/bits/types.h" 2 3 4 + + +typedef unsigned long int __dev_t; +typedef unsigned int __uid_t; +typedef unsigned int __gid_t; +typedef unsigned long int __ino_t; +typedef unsigned long int __ino64_t; +typedef unsigned int __mode_t; +typedef unsigned long int __nlink_t; +typedef long int __off_t; +typedef long int __off64_t; +typedef int __pid_t; +typedef struct { int __val[2]; } __fsid_t; +typedef long int __clock_t; +typedef unsigned long int __rlim_t; +typedef unsigned long int __rlim64_t; +typedef unsigned int __id_t; +typedef long int __time_t; +typedef unsigned int __useconds_t; +typedef long int __suseconds_t; +typedef long int __suseconds64_t; + +typedef int __daddr_t; +typedef int __key_t; + + +typedef int __clockid_t; + + +typedef void * __timer_t; + + +typedef long int __blksize_t; + + + + +typedef long int __blkcnt_t; +typedef long int __blkcnt64_t; + + +typedef unsigned long int __fsblkcnt_t; +typedef unsigned long int __fsblkcnt64_t; + + +typedef unsigned long int __fsfilcnt_t; +typedef unsigned long int __fsfilcnt64_t; + + +typedef long int __fsword_t; + +typedef long int __ssize_t; + + +typedef long int __syscall_slong_t; + +typedef unsigned long int __syscall_ulong_t; + + + +typedef __off64_t __loff_t; +typedef char *__caddr_t; + + +typedef long int __intptr_t; + + +typedef unsigned int __socklen_t; + + + + +typedef int __sig_atomic_t; +# 39 "/usr/include/stdio.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/types/__fpos_t.h" 1 3 4 + + + + +# 1 "/usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h" 1 3 4 +# 13 "/usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h" 3 4 +typedef struct +{ + int __count; + union + { + unsigned int __wch; + char __wchb[4]; + } __value; +} __mbstate_t; +# 6 "/usr/include/x86_64-linux-gnu/bits/types/__fpos_t.h" 2 3 4 + + + + +typedef struct _G_fpos_t +{ + __off_t __pos; + __mbstate_t __state; +} __fpos_t; +# 40 "/usr/include/stdio.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/types/__fpos64_t.h" 1 3 4 +# 10 "/usr/include/x86_64-linux-gnu/bits/types/__fpos64_t.h" 3 4 +typedef struct _G_fpos64_t +{ + __off64_t __pos; + __mbstate_t __state; +} __fpos64_t; +# 41 "/usr/include/stdio.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/types/__FILE.h" 1 3 4 + + + +struct _IO_FILE; +typedef struct _IO_FILE __FILE; +# 42 "/usr/include/stdio.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/types/FILE.h" 1 3 4 + + + +struct _IO_FILE; + + +typedef struct _IO_FILE FILE; +# 43 "/usr/include/stdio.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/types/struct_FILE.h" 1 3 4 +# 35 "/usr/include/x86_64-linux-gnu/bits/types/struct_FILE.h" 3 4 +struct _IO_FILE; +struct _IO_marker; +struct _IO_codecvt; +struct _IO_wide_data; + + + + +typedef void _IO_lock_t; + + + + + +struct _IO_FILE +{ + int _flags; + + + char *_IO_read_ptr; + char *_IO_read_end; + char *_IO_read_base; + char *_IO_write_base; + char *_IO_write_ptr; + char *_IO_write_end; + char *_IO_buf_base; + char *_IO_buf_end; + + + char *_IO_save_base; + char *_IO_backup_base; + char *_IO_save_end; + + struct _IO_marker *_markers; + + struct _IO_FILE *_chain; + + int _fileno; + int _flags2; + __off_t _old_offset; + + + unsigned short _cur_column; + signed char _vtable_offset; + char _shortbuf[1]; + + _IO_lock_t *_lock; + + + + + + + + __off64_t _offset; + + struct _IO_codecvt *_codecvt; + struct _IO_wide_data *_wide_data; + struct _IO_FILE *_freeres_list; + void *_freeres_buf; + size_t __pad5; + int _mode; + + char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)]; +}; +# 44 "/usr/include/stdio.h" 2 3 4 +# 52 "/usr/include/stdio.h" 3 4 +typedef __gnuc_va_list va_list; +# 63 "/usr/include/stdio.h" 3 4 +typedef __off_t off_t; +# 77 "/usr/include/stdio.h" 3 4 +typedef __ssize_t ssize_t; + + + + + + +typedef __fpos_t fpos_t; +# 133 "/usr/include/stdio.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/stdio_lim.h" 1 3 4 +# 134 "/usr/include/stdio.h" 2 3 4 + + + +extern FILE *stdin; +extern FILE *stdout; +extern FILE *stderr; + + + + + + +extern int remove (const char *__filename) __attribute__ ((__nothrow__ , __leaf__)); + +extern int rename (const char *__old, const char *__new) __attribute__ ((__nothrow__ , __leaf__)); + + + +extern int renameat (int __oldfd, const char *__old, int __newfd, + const char *__new) __attribute__ ((__nothrow__ , __leaf__)); +# 173 "/usr/include/stdio.h" 3 4 +extern FILE *tmpfile (void) ; +# 187 "/usr/include/stdio.h" 3 4 +extern char *tmpnam (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ; + + + + +extern char *tmpnam_r (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ; +# 204 "/usr/include/stdio.h" 3 4 +extern char *tempnam (const char *__dir, const char *__pfx) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) ; + + + + + + + +extern int fclose (FILE *__stream); + + + + +extern int fflush (FILE *__stream); +# 227 "/usr/include/stdio.h" 3 4 +extern int fflush_unlocked (FILE *__stream); +# 246 "/usr/include/stdio.h" 3 4 +extern FILE *fopen (const char *__restrict __filename, + const char *__restrict __modes) ; + + + + +extern FILE *freopen (const char *__restrict __filename, + const char *__restrict __modes, + FILE *__restrict __stream) ; +# 279 "/usr/include/stdio.h" 3 4 +extern FILE *fdopen (int __fd, const char *__modes) __attribute__ ((__nothrow__ , __leaf__)) ; +# 292 "/usr/include/stdio.h" 3 4 +extern FILE *fmemopen (void *__s, size_t __len, const char *__modes) + __attribute__ ((__nothrow__ , __leaf__)) ; + + + + +extern FILE *open_memstream (char **__bufloc, size_t *__sizeloc) __attribute__ ((__nothrow__ , __leaf__)) ; + + + + + +extern void setbuf (FILE *__restrict __stream, char *__restrict __buf) __attribute__ ((__nothrow__ , __leaf__)); + + + +extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf, + int __modes, size_t __n) __attribute__ ((__nothrow__ , __leaf__)); + + + + +extern void setbuffer (FILE *__restrict __stream, char *__restrict __buf, + size_t __size) __attribute__ ((__nothrow__ , __leaf__)); + + +extern void setlinebuf (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); + + + + + + + +extern int fprintf (FILE *__restrict __stream, + const char *__restrict __format, ...); + + + + +extern int printf (const char *__restrict __format, ...); + +extern int sprintf (char *__restrict __s, + const char *__restrict __format, ...) __attribute__ ((__nothrow__)); + + + + + +extern int vfprintf (FILE *__restrict __s, const char *__restrict __format, + __gnuc_va_list __arg); + + + + +extern int vprintf (const char *__restrict __format, __gnuc_va_list __arg); + +extern int vsprintf (char *__restrict __s, const char *__restrict __format, + __gnuc_va_list __arg) __attribute__ ((__nothrow__)); + + + +extern int snprintf (char *__restrict __s, size_t __maxlen, + const char *__restrict __format, ...) + __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 4))); + +extern int vsnprintf (char *__restrict __s, size_t __maxlen, + const char *__restrict __format, __gnuc_va_list __arg) + __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 0))); +# 379 "/usr/include/stdio.h" 3 4 +extern int vdprintf (int __fd, const char *__restrict __fmt, + __gnuc_va_list __arg) + __attribute__ ((__format__ (__printf__, 2, 0))); +extern int dprintf (int __fd, const char *__restrict __fmt, ...) + __attribute__ ((__format__ (__printf__, 2, 3))); + + + + + + + +extern int fscanf (FILE *__restrict __stream, + const char *__restrict __format, ...) ; + + + + +extern int scanf (const char *__restrict __format, ...) ; + +extern int sscanf (const char *__restrict __s, + const char *__restrict __format, ...) __attribute__ ((__nothrow__ , __leaf__)); + + + + + +# 1 "/usr/include/x86_64-linux-gnu/bits/floatn.h" 1 3 4 +# 120 "/usr/include/x86_64-linux-gnu/bits/floatn.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/floatn-common.h" 1 3 4 +# 24 "/usr/include/x86_64-linux-gnu/bits/floatn-common.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/long-double.h" 1 3 4 +# 25 "/usr/include/x86_64-linux-gnu/bits/floatn-common.h" 2 3 4 +# 121 "/usr/include/x86_64-linux-gnu/bits/floatn.h" 2 3 4 +# 407 "/usr/include/stdio.h" 2 3 4 + + + +extern int fscanf (FILE *__restrict __stream, const char *__restrict __format, ...) __asm__ ("" "__isoc99_fscanf") + + ; +extern int scanf (const char *__restrict __format, ...) __asm__ ("" "__isoc99_scanf") + ; +extern int sscanf (const char *__restrict __s, const char *__restrict __format, ...) __asm__ ("" "__isoc99_sscanf") __attribute__ ((__nothrow__ , __leaf__)) + + ; +# 435 "/usr/include/stdio.h" 3 4 +extern int vfscanf (FILE *__restrict __s, const char *__restrict __format, + __gnuc_va_list __arg) + __attribute__ ((__format__ (__scanf__, 2, 0))) ; + + + + + +extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg) + __attribute__ ((__format__ (__scanf__, 1, 0))) ; + + +extern int vsscanf (const char *__restrict __s, + const char *__restrict __format, __gnuc_va_list __arg) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__format__ (__scanf__, 2, 0))); + + + + + +extern int vfscanf (FILE *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vfscanf") + + + + __attribute__ ((__format__ (__scanf__, 2, 0))) ; +extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vscanf") + + __attribute__ ((__format__ (__scanf__, 1, 0))) ; +extern int vsscanf (const char *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vsscanf") __attribute__ ((__nothrow__ , __leaf__)) + + + + __attribute__ ((__format__ (__scanf__, 2, 0))); +# 489 "/usr/include/stdio.h" 3 4 +extern int fgetc (FILE *__stream); +extern int getc (FILE *__stream); + + + + + +extern int getchar (void); + + + + + + +extern int getc_unlocked (FILE *__stream); +extern int getchar_unlocked (void); +# 514 "/usr/include/stdio.h" 3 4 +extern int fgetc_unlocked (FILE *__stream); +# 525 "/usr/include/stdio.h" 3 4 +extern int fputc (int __c, FILE *__stream); +extern int putc (int __c, FILE *__stream); + + + + + +extern int putchar (int __c); +# 541 "/usr/include/stdio.h" 3 4 +extern int fputc_unlocked (int __c, FILE *__stream); + + + + + + + +extern int putc_unlocked (int __c, FILE *__stream); +extern int putchar_unlocked (int __c); + + + + + + +extern int getw (FILE *__stream); + + +extern int putw (int __w, FILE *__stream); + + + + + + + +extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream) + __attribute__ ((__access__ (__write_only__, 1, 2))); +# 608 "/usr/include/stdio.h" 3 4 +extern __ssize_t __getdelim (char **__restrict __lineptr, + size_t *__restrict __n, int __delimiter, + FILE *__restrict __stream) ; +extern __ssize_t getdelim (char **__restrict __lineptr, + size_t *__restrict __n, int __delimiter, + FILE *__restrict __stream) ; + + + + + + + +extern __ssize_t getline (char **__restrict __lineptr, + size_t *__restrict __n, + FILE *__restrict __stream) ; + + + + + + + +extern int fputs (const char *__restrict __s, FILE *__restrict __stream); + + + + + +extern int puts (const char *__s); + + + + + + +extern int ungetc (int __c, FILE *__stream); + + + + + + +extern size_t fread (void *__restrict __ptr, size_t __size, + size_t __n, FILE *__restrict __stream) ; + + + + +extern size_t fwrite (const void *__restrict __ptr, size_t __size, + size_t __n, FILE *__restrict __s); +# 678 "/usr/include/stdio.h" 3 4 +extern size_t fread_unlocked (void *__restrict __ptr, size_t __size, + size_t __n, FILE *__restrict __stream) ; +extern size_t fwrite_unlocked (const void *__restrict __ptr, size_t __size, + size_t __n, FILE *__restrict __stream); + + + + + + + +extern int fseek (FILE *__stream, long int __off, int __whence); + + + + +extern long int ftell (FILE *__stream) ; + + + + +extern void rewind (FILE *__stream); +# 712 "/usr/include/stdio.h" 3 4 +extern int fseeko (FILE *__stream, __off_t __off, int __whence); + + + + +extern __off_t ftello (FILE *__stream) ; +# 736 "/usr/include/stdio.h" 3 4 +extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos); + + + + +extern int fsetpos (FILE *__stream, const fpos_t *__pos); +# 762 "/usr/include/stdio.h" 3 4 +extern void clearerr (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); + +extern int feof (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; + +extern int ferror (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; + + + +extern void clearerr_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); +extern int feof_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; +extern int ferror_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; + + + + + + + +extern void perror (const char *__s); + + + + +extern int fileno (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; + + + + +extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; +# 799 "/usr/include/stdio.h" 3 4 +extern FILE *popen (const char *__command, const char *__modes) ; + + + + + +extern int pclose (FILE *__stream); + + + + + +extern char *ctermid (char *__s) __attribute__ ((__nothrow__ , __leaf__)); +# 839 "/usr/include/stdio.h" 3 4 +extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); + + + +extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ; + + +extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)); +# 857 "/usr/include/stdio.h" 3 4 +extern int __uflow (FILE *); +extern int __overflow (FILE *, int); + + + + +# 1 "/usr/include/x86_64-linux-gnu/bits/stdio.h" 1 3 4 +# 38 "/usr/include/x86_64-linux-gnu/bits/stdio.h" 3 4 +extern __inline __attribute__ ((__gnu_inline__)) int +vprintf (const char *__restrict __fmt, __gnuc_va_list __arg) +{ + return vfprintf (stdout, __fmt, __arg); +} + + + +extern __inline __attribute__ ((__gnu_inline__)) int +getchar (void) +{ + return getc (stdin); +} + + + + +extern __inline __attribute__ ((__gnu_inline__)) int +fgetc_unlocked (FILE *__fp) +{ + return (__builtin_expect (((__fp)->_IO_read_ptr >= (__fp)->_IO_read_end), 0) ? __uflow (__fp) : *(unsigned char *) (__fp)->_IO_read_ptr++); +} + + + + + +extern __inline __attribute__ ((__gnu_inline__)) int +getc_unlocked (FILE *__fp) +{ + return (__builtin_expect (((__fp)->_IO_read_ptr >= (__fp)->_IO_read_end), 0) ? __uflow (__fp) : *(unsigned char *) (__fp)->_IO_read_ptr++); +} + + +extern __inline __attribute__ ((__gnu_inline__)) int +getchar_unlocked (void) +{ + return (__builtin_expect (((stdin)->_IO_read_ptr >= (stdin)->_IO_read_end), 0) ? __uflow (stdin) : *(unsigned char *) (stdin)->_IO_read_ptr++); +} + + + + +extern __inline __attribute__ ((__gnu_inline__)) int +putchar (int __c) +{ + return putc (__c, stdout); +} + + + + +extern __inline __attribute__ ((__gnu_inline__)) int +fputc_unlocked (int __c, FILE *__stream) +{ + return (__builtin_expect (((__stream)->_IO_write_ptr >= (__stream)->_IO_write_end), 0) ? __overflow (__stream, (unsigned char) (__c)) : (unsigned char) (*(__stream)->_IO_write_ptr++ = (__c))); +} + + + + + +extern __inline __attribute__ ((__gnu_inline__)) int +putc_unlocked (int __c, FILE *__stream) +{ + return (__builtin_expect (((__stream)->_IO_write_ptr >= (__stream)->_IO_write_end), 0) ? __overflow (__stream, (unsigned char) (__c)) : (unsigned char) (*(__stream)->_IO_write_ptr++ = (__c))); +} + + +extern __inline __attribute__ ((__gnu_inline__)) int +putchar_unlocked (int __c) +{ + return (__builtin_expect (((stdout)->_IO_write_ptr >= (stdout)->_IO_write_end), 0) ? __overflow (stdout, (unsigned char) (__c)) : (unsigned char) (*(stdout)->_IO_write_ptr++ = (__c))); +} +# 127 "/usr/include/x86_64-linux-gnu/bits/stdio.h" 3 4 +extern __inline __attribute__ ((__gnu_inline__)) int +__attribute__ ((__nothrow__ , __leaf__)) feof_unlocked (FILE *__stream) +{ + return (((__stream)->_flags & 0x0010) != 0); +} + + +extern __inline __attribute__ ((__gnu_inline__)) int +__attribute__ ((__nothrow__ , __leaf__)) ferror_unlocked (FILE *__stream) +{ + return (((__stream)->_flags & 0x0020) != 0); +} +# 864 "/usr/include/stdio.h" 2 3 4 +# 874 "/usr/include/stdio.h" 3 4 + +# 2 "code.c" 2 +# 1 "/usr/include/stdlib.h" 1 3 4 +# 25 "/usr/include/stdlib.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/libc-header-start.h" 1 3 4 +# 26 "/usr/include/stdlib.h" 2 3 4 + + + + + +# 1 "/usr/lib/gcc/x86_64-linux-gnu/11/include/stddef.h" 1 3 4 +# 321 "/usr/lib/gcc/x86_64-linux-gnu/11/include/stddef.h" 3 4 +typedef int wchar_t; +# 32 "/usr/include/stdlib.h" 2 3 4 + + + + + + + +# 1 "/usr/include/x86_64-linux-gnu/bits/waitflags.h" 1 3 4 +# 52 "/usr/include/x86_64-linux-gnu/bits/waitflags.h" 3 4 +typedef enum +{ + P_ALL, + P_PID, + P_PGID +} idtype_t; +# 40 "/usr/include/stdlib.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/waitstatus.h" 1 3 4 +# 41 "/usr/include/stdlib.h" 2 3 4 +# 58 "/usr/include/stdlib.h" 3 4 +typedef struct + { + int quot; + int rem; + } div_t; + + + +typedef struct + { + long int quot; + long int rem; + } ldiv_t; + + + + + +__extension__ typedef struct + { + long long int quot; + long long int rem; + } lldiv_t; +# 97 "/usr/include/stdlib.h" 3 4 +extern size_t __ctype_get_mb_cur_max (void) __attribute__ ((__nothrow__ , __leaf__)) ; + + + +extern double atof (const char *__nptr) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1))) ; + +extern int atoi (const char *__nptr) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1))) ; + +extern long int atol (const char *__nptr) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1))) ; + + + +__extension__ extern long long int atoll (const char *__nptr) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1))) ; + + + +extern double strtod (const char *__restrict __nptr, + char **__restrict __endptr) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + + +extern float strtof (const char *__restrict __nptr, + char **__restrict __endptr) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + +extern long double strtold (const char *__restrict __nptr, + char **__restrict __endptr) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); +# 176 "/usr/include/stdlib.h" 3 4 +extern long int strtol (const char *__restrict __nptr, + char **__restrict __endptr, int __base) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + +extern unsigned long int strtoul (const char *__restrict __nptr, + char **__restrict __endptr, int __base) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + + +__extension__ +extern long long int strtoq (const char *__restrict __nptr, + char **__restrict __endptr, int __base) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + +__extension__ +extern unsigned long long int strtouq (const char *__restrict __nptr, + char **__restrict __endptr, int __base) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + + + +__extension__ +extern long long int strtoll (const char *__restrict __nptr, + char **__restrict __endptr, int __base) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + +__extension__ +extern unsigned long long int strtoull (const char *__restrict __nptr, + char **__restrict __endptr, int __base) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); +# 360 "/usr/include/stdlib.h" 3 4 +extern __inline __attribute__ ((__gnu_inline__)) int +__attribute__ ((__nothrow__ , __leaf__)) atoi (const char *__nptr) +{ + return (int) strtol (__nptr, (char **) ((void *)0), 10); +} +extern __inline __attribute__ ((__gnu_inline__)) long int +__attribute__ ((__nothrow__ , __leaf__)) atol (const char *__nptr) +{ + return strtol (__nptr, (char **) ((void *)0), 10); +} + + +__extension__ extern __inline __attribute__ ((__gnu_inline__)) long long int +__attribute__ ((__nothrow__ , __leaf__)) atoll (const char *__nptr) +{ + return strtoll (__nptr, (char **) ((void *)0), 10); +} +# 385 "/usr/include/stdlib.h" 3 4 +extern char *l64a (long int __n) __attribute__ ((__nothrow__ , __leaf__)) ; + + +extern long int a64l (const char *__s) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1))) ; + + + + +# 1 "/usr/include/x86_64-linux-gnu/sys/types.h" 1 3 4 +# 27 "/usr/include/x86_64-linux-gnu/sys/types.h" 3 4 + + + + + + +typedef __u_char u_char; +typedef __u_short u_short; +typedef __u_int u_int; +typedef __u_long u_long; +typedef __quad_t quad_t; +typedef __u_quad_t u_quad_t; +typedef __fsid_t fsid_t; + + +typedef __loff_t loff_t; + + + + +typedef __ino_t ino_t; +# 59 "/usr/include/x86_64-linux-gnu/sys/types.h" 3 4 +typedef __dev_t dev_t; + + + + +typedef __gid_t gid_t; + + + + +typedef __mode_t mode_t; + + + + +typedef __nlink_t nlink_t; + + + + +typedef __uid_t uid_t; +# 97 "/usr/include/x86_64-linux-gnu/sys/types.h" 3 4 +typedef __pid_t pid_t; + + + + + +typedef __id_t id_t; +# 114 "/usr/include/x86_64-linux-gnu/sys/types.h" 3 4 +typedef __daddr_t daddr_t; +typedef __caddr_t caddr_t; + + + + + +typedef __key_t key_t; + + + + +# 1 "/usr/include/x86_64-linux-gnu/bits/types/clock_t.h" 1 3 4 + + + + + + +typedef __clock_t clock_t; +# 127 "/usr/include/x86_64-linux-gnu/sys/types.h" 2 3 4 + +# 1 "/usr/include/x86_64-linux-gnu/bits/types/clockid_t.h" 1 3 4 + + + + + + +typedef __clockid_t clockid_t; +# 129 "/usr/include/x86_64-linux-gnu/sys/types.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/types/time_t.h" 1 3 4 + + + + + + +typedef __time_t time_t; +# 130 "/usr/include/x86_64-linux-gnu/sys/types.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/types/timer_t.h" 1 3 4 + + + + + + +typedef __timer_t timer_t; +# 131 "/usr/include/x86_64-linux-gnu/sys/types.h" 2 3 4 +# 144 "/usr/include/x86_64-linux-gnu/sys/types.h" 3 4 +# 1 "/usr/lib/gcc/x86_64-linux-gnu/11/include/stddef.h" 1 3 4 +# 145 "/usr/include/x86_64-linux-gnu/sys/types.h" 2 3 4 + + + +typedef unsigned long int ulong; +typedef unsigned short int ushort; +typedef unsigned int uint; + + + + +# 1 "/usr/include/x86_64-linux-gnu/bits/stdint-intn.h" 1 3 4 +# 24 "/usr/include/x86_64-linux-gnu/bits/stdint-intn.h" 3 4 +typedef __int8_t int8_t; +typedef __int16_t int16_t; +typedef __int32_t int32_t; +typedef __int64_t int64_t; +# 156 "/usr/include/x86_64-linux-gnu/sys/types.h" 2 3 4 + + +typedef __uint8_t u_int8_t; +typedef __uint16_t u_int16_t; +typedef __uint32_t u_int32_t; +typedef __uint64_t u_int64_t; + + +typedef int register_t __attribute__ ((__mode__ (__word__))); +# 176 "/usr/include/x86_64-linux-gnu/sys/types.h" 3 4 +# 1 "/usr/include/endian.h" 1 3 4 +# 24 "/usr/include/endian.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/endian.h" 1 3 4 +# 35 "/usr/include/x86_64-linux-gnu/bits/endian.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/endianness.h" 1 3 4 +# 36 "/usr/include/x86_64-linux-gnu/bits/endian.h" 2 3 4 +# 25 "/usr/include/endian.h" 2 3 4 +# 35 "/usr/include/endian.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/byteswap.h" 1 3 4 +# 33 "/usr/include/x86_64-linux-gnu/bits/byteswap.h" 3 4 +static __inline __uint16_t +__bswap_16 (__uint16_t __bsx) +{ + + return __builtin_bswap16 (__bsx); + + + +} + + + + + + +static __inline __uint32_t +__bswap_32 (__uint32_t __bsx) +{ + + return __builtin_bswap32 (__bsx); + + + +} +# 69 "/usr/include/x86_64-linux-gnu/bits/byteswap.h" 3 4 +__extension__ static __inline __uint64_t +__bswap_64 (__uint64_t __bsx) +{ + + return __builtin_bswap64 (__bsx); + + + +} +# 36 "/usr/include/endian.h" 2 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/uintn-identity.h" 1 3 4 +# 32 "/usr/include/x86_64-linux-gnu/bits/uintn-identity.h" 3 4 +static __inline __uint16_t +__uint16_identity (__uint16_t __x) +{ + return __x; +} + +static __inline __uint32_t +__uint32_identity (__uint32_t __x) +{ + return __x; +} + +static __inline __uint64_t +__uint64_identity (__uint64_t __x) +{ + return __x; +} +# 37 "/usr/include/endian.h" 2 3 4 +# 177 "/usr/include/x86_64-linux-gnu/sys/types.h" 2 3 4 + + +# 1 "/usr/include/x86_64-linux-gnu/sys/select.h" 1 3 4 +# 30 "/usr/include/x86_64-linux-gnu/sys/select.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/select.h" 1 3 4 +# 31 "/usr/include/x86_64-linux-gnu/sys/select.h" 2 3 4 + + +# 1 "/usr/include/x86_64-linux-gnu/bits/types/sigset_t.h" 1 3 4 + + + +# 1 "/usr/include/x86_64-linux-gnu/bits/types/__sigset_t.h" 1 3 4 + + + + +typedef struct +{ + unsigned long int __val[(1024 / (8 * sizeof (unsigned long int)))]; +} __sigset_t; +# 5 "/usr/include/x86_64-linux-gnu/bits/types/sigset_t.h" 2 3 4 + + +typedef __sigset_t sigset_t; +# 34 "/usr/include/x86_64-linux-gnu/sys/select.h" 2 3 4 + + + +# 1 "/usr/include/x86_64-linux-gnu/bits/types/struct_timeval.h" 1 3 4 + + + + + + + +struct timeval +{ + __time_t tv_sec; + __suseconds_t tv_usec; +}; +# 38 "/usr/include/x86_64-linux-gnu/sys/select.h" 2 3 4 + +# 1 "/usr/include/x86_64-linux-gnu/bits/types/struct_timespec.h" 1 3 4 +# 10 "/usr/include/x86_64-linux-gnu/bits/types/struct_timespec.h" 3 4 +struct timespec +{ + __time_t tv_sec; + + + + __syscall_slong_t tv_nsec; +# 26 "/usr/include/x86_64-linux-gnu/bits/types/struct_timespec.h" 3 4 +}; +# 40 "/usr/include/x86_64-linux-gnu/sys/select.h" 2 3 4 + + + +typedef __suseconds_t suseconds_t; + + + + + +typedef long int __fd_mask; +# 59 "/usr/include/x86_64-linux-gnu/sys/select.h" 3 4 +typedef struct + { + + + + + + + __fd_mask __fds_bits[1024 / (8 * (int) sizeof (__fd_mask))]; + + + } fd_set; + + + + + + +typedef __fd_mask fd_mask; +# 91 "/usr/include/x86_64-linux-gnu/sys/select.h" 3 4 + +# 101 "/usr/include/x86_64-linux-gnu/sys/select.h" 3 4 +extern int select (int __nfds, fd_set *__restrict __readfds, + fd_set *__restrict __writefds, + fd_set *__restrict __exceptfds, + struct timeval *__restrict __timeout); +# 113 "/usr/include/x86_64-linux-gnu/sys/select.h" 3 4 +extern int pselect (int __nfds, fd_set *__restrict __readfds, + fd_set *__restrict __writefds, + fd_set *__restrict __exceptfds, + const struct timespec *__restrict __timeout, + const __sigset_t *__restrict __sigmask); +# 126 "/usr/include/x86_64-linux-gnu/sys/select.h" 3 4 + +# 180 "/usr/include/x86_64-linux-gnu/sys/types.h" 2 3 4 + + + + + +typedef __blksize_t blksize_t; + + + + + + +typedef __blkcnt_t blkcnt_t; + + + +typedef __fsblkcnt_t fsblkcnt_t; + + + +typedef __fsfilcnt_t fsfilcnt_t; +# 227 "/usr/include/x86_64-linux-gnu/sys/types.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/pthreadtypes.h" 1 3 4 +# 23 "/usr/include/x86_64-linux-gnu/bits/pthreadtypes.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/thread-shared-types.h" 1 3 4 +# 44 "/usr/include/x86_64-linux-gnu/bits/thread-shared-types.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/pthreadtypes-arch.h" 1 3 4 +# 21 "/usr/include/x86_64-linux-gnu/bits/pthreadtypes-arch.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4 +# 22 "/usr/include/x86_64-linux-gnu/bits/pthreadtypes-arch.h" 2 3 4 +# 45 "/usr/include/x86_64-linux-gnu/bits/thread-shared-types.h" 2 3 4 + + + + +typedef struct __pthread_internal_list +{ + struct __pthread_internal_list *__prev; + struct __pthread_internal_list *__next; +} __pthread_list_t; + +typedef struct __pthread_internal_slist +{ + struct __pthread_internal_slist *__next; +} __pthread_slist_t; +# 74 "/usr/include/x86_64-linux-gnu/bits/thread-shared-types.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/struct_mutex.h" 1 3 4 +# 22 "/usr/include/x86_64-linux-gnu/bits/struct_mutex.h" 3 4 +struct __pthread_mutex_s +{ + int __lock; + unsigned int __count; + int __owner; + + unsigned int __nusers; + + + + int __kind; + + short __spins; + short __elision; + __pthread_list_t __list; +# 53 "/usr/include/x86_64-linux-gnu/bits/struct_mutex.h" 3 4 +}; +# 75 "/usr/include/x86_64-linux-gnu/bits/thread-shared-types.h" 2 3 4 +# 87 "/usr/include/x86_64-linux-gnu/bits/thread-shared-types.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/struct_rwlock.h" 1 3 4 +# 23 "/usr/include/x86_64-linux-gnu/bits/struct_rwlock.h" 3 4 +struct __pthread_rwlock_arch_t +{ + unsigned int __readers; + unsigned int __writers; + unsigned int __wrphase_futex; + unsigned int __writers_futex; + unsigned int __pad3; + unsigned int __pad4; + + int __cur_writer; + int __shared; + signed char __rwelision; + + + + + unsigned char __pad1[7]; + + + unsigned long int __pad2; + + + unsigned int __flags; +# 55 "/usr/include/x86_64-linux-gnu/bits/struct_rwlock.h" 3 4 +}; +# 88 "/usr/include/x86_64-linux-gnu/bits/thread-shared-types.h" 2 3 4 + + + + +struct __pthread_cond_s +{ + __extension__ union + { + __extension__ unsigned long long int __wseq; + struct + { + unsigned int __low; + unsigned int __high; + } __wseq32; + }; + __extension__ union + { + __extension__ unsigned long long int __g1_start; + struct + { + unsigned int __low; + unsigned int __high; + } __g1_start32; + }; + unsigned int __g_refs[2] ; + unsigned int __g_size[2]; + unsigned int __g1_orig_size; + unsigned int __wrefs; + unsigned int __g_signals[2]; +}; + +typedef unsigned int __tss_t; +typedef unsigned long int __thrd_t; + +typedef struct +{ + int __data ; +} __once_flag; +# 24 "/usr/include/x86_64-linux-gnu/bits/pthreadtypes.h" 2 3 4 + + + +typedef unsigned long int pthread_t; + + + + +typedef union +{ + char __size[4]; + int __align; +} pthread_mutexattr_t; + + + + +typedef union +{ + char __size[4]; + int __align; +} pthread_condattr_t; + + + +typedef unsigned int pthread_key_t; + + + +typedef int pthread_once_t; + + +union pthread_attr_t +{ + char __size[56]; + long int __align; +}; + +typedef union pthread_attr_t pthread_attr_t; + + + + +typedef union +{ + struct __pthread_mutex_s __data; + char __size[40]; + long int __align; +} pthread_mutex_t; + + +typedef union +{ + struct __pthread_cond_s __data; + char __size[48]; + __extension__ long long int __align; +} pthread_cond_t; + + + + + +typedef union +{ + struct __pthread_rwlock_arch_t __data; + char __size[56]; + long int __align; +} pthread_rwlock_t; + +typedef union +{ + char __size[8]; + long int __align; +} pthread_rwlockattr_t; + + + + + +typedef volatile int pthread_spinlock_t; + + + + +typedef union +{ + char __size[32]; + long int __align; +} pthread_barrier_t; + +typedef union +{ + char __size[4]; + int __align; +} pthread_barrierattr_t; +# 228 "/usr/include/x86_64-linux-gnu/sys/types.h" 2 3 4 + + + +# 395 "/usr/include/stdlib.h" 2 3 4 + + + + + + +extern long int random (void) __attribute__ ((__nothrow__ , __leaf__)); + + +extern void srandom (unsigned int __seed) __attribute__ ((__nothrow__ , __leaf__)); + + + + + +extern char *initstate (unsigned int __seed, char *__statebuf, + size_t __statelen) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (2))); + + + +extern char *setstate (char *__statebuf) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + + + + + + +struct random_data + { + int32_t *fptr; + int32_t *rptr; + int32_t *state; + int rand_type; + int rand_deg; + int rand_sep; + int32_t *end_ptr; + }; + +extern int random_r (struct random_data *__restrict __buf, + int32_t *__restrict __result) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); + +extern int srandom_r (unsigned int __seed, struct random_data *__buf) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (2))); + +extern int initstate_r (unsigned int __seed, char *__restrict __statebuf, + size_t __statelen, + struct random_data *__restrict __buf) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (2, 4))); + +extern int setstate_r (char *__restrict __statebuf, + struct random_data *__restrict __buf) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); + + + + + +extern int rand (void) __attribute__ ((__nothrow__ , __leaf__)); + +extern void srand (unsigned int __seed) __attribute__ ((__nothrow__ , __leaf__)); + + + +extern int rand_r (unsigned int *__seed) __attribute__ ((__nothrow__ , __leaf__)); + + + + + + + +extern double drand48 (void) __attribute__ ((__nothrow__ , __leaf__)); +extern double erand48 (unsigned short int __xsubi[3]) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + +extern long int lrand48 (void) __attribute__ ((__nothrow__ , __leaf__)); +extern long int nrand48 (unsigned short int __xsubi[3]) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + +extern long int mrand48 (void) __attribute__ ((__nothrow__ , __leaf__)); +extern long int jrand48 (unsigned short int __xsubi[3]) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + +extern void srand48 (long int __seedval) __attribute__ ((__nothrow__ , __leaf__)); +extern unsigned short int *seed48 (unsigned short int __seed16v[3]) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); +extern void lcong48 (unsigned short int __param[7]) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + + + + +struct drand48_data + { + unsigned short int __x[3]; + unsigned short int __old_x[3]; + unsigned short int __c; + unsigned short int __init; + __extension__ unsigned long long int __a; + + }; + + +extern int drand48_r (struct drand48_data *__restrict __buffer, + double *__restrict __result) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); +extern int erand48_r (unsigned short int __xsubi[3], + struct drand48_data *__restrict __buffer, + double *__restrict __result) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); + + +extern int lrand48_r (struct drand48_data *__restrict __buffer, + long int *__restrict __result) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); +extern int nrand48_r (unsigned short int __xsubi[3], + struct drand48_data *__restrict __buffer, + long int *__restrict __result) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); + + +extern int mrand48_r (struct drand48_data *__restrict __buffer, + long int *__restrict __result) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); +extern int jrand48_r (unsigned short int __xsubi[3], + struct drand48_data *__restrict __buffer, + long int *__restrict __result) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); + + +extern int srand48_r (long int __seedval, struct drand48_data *__buffer) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (2))); + +extern int seed48_r (unsigned short int __seed16v[3], + struct drand48_data *__buffer) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); + +extern int lcong48_r (unsigned short int __param[7], + struct drand48_data *__buffer) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); + + + + +extern void *malloc (size_t __size) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) + __attribute__ ((__alloc_size__ (1))) ; + +extern void *calloc (size_t __nmemb, size_t __size) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) __attribute__ ((__alloc_size__ (1, 2))) ; + + + + + + +extern void *realloc (void *__ptr, size_t __size) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__warn_unused_result__)) __attribute__ ((__alloc_size__ (2))); + + + + + + + +extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__warn_unused_result__)) + __attribute__ ((__alloc_size__ (2, 3))); + + + +extern void free (void *__ptr) __attribute__ ((__nothrow__ , __leaf__)); + + +# 1 "/usr/include/alloca.h" 1 3 4 +# 24 "/usr/include/alloca.h" 3 4 +# 1 "/usr/lib/gcc/x86_64-linux-gnu/11/include/stddef.h" 1 3 4 +# 25 "/usr/include/alloca.h" 2 3 4 + + + + + + + +extern void *alloca (size_t __size) __attribute__ ((__nothrow__ , __leaf__)); + + + + + + +# 569 "/usr/include/stdlib.h" 2 3 4 + + + + + +extern void *valloc (size_t __size) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) + __attribute__ ((__alloc_size__ (1))) ; + + + + +extern int posix_memalign (void **__memptr, size_t __alignment, size_t __size) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))) ; + + + + +extern void *aligned_alloc (size_t __alignment, size_t __size) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) __attribute__ ((__alloc_size__ (2))) ; + + + +extern void abort (void) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__noreturn__)); + + + +extern int atexit (void (*__func) (void)) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + + + + + + +extern int at_quick_exit (void (*__func) (void)) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + + + + + +extern int on_exit (void (*__func) (int __status, void *__arg), void *__arg) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + + + + +extern void exit (int __status) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__noreturn__)); + + + + + +extern void quick_exit (int __status) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__noreturn__)); + + + + + +extern void _Exit (int __status) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__noreturn__)); + + + + +extern char *getenv (const char *__name) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))) ; +# 647 "/usr/include/stdlib.h" 3 4 +extern int putenv (char *__string) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + + + + +extern int setenv (const char *__name, const char *__value, int __replace) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (2))); + + +extern int unsetenv (const char *__name) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); + + + + + + +extern int clearenv (void) __attribute__ ((__nothrow__ , __leaf__)); +# 675 "/usr/include/stdlib.h" 3 4 +extern char *mktemp (char *__template) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); +# 688 "/usr/include/stdlib.h" 3 4 +extern int mkstemp (char *__template) __attribute__ ((__nonnull__ (1))) ; +# 710 "/usr/include/stdlib.h" 3 4 +extern int mkstemps (char *__template, int __suffixlen) __attribute__ ((__nonnull__ (1))) ; +# 731 "/usr/include/stdlib.h" 3 4 +extern char *mkdtemp (char *__template) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))) ; +# 784 "/usr/include/stdlib.h" 3 4 +extern int system (const char *__command) ; +# 800 "/usr/include/stdlib.h" 3 4 +extern char *realpath (const char *__restrict __name, + char *__restrict __resolved) __attribute__ ((__nothrow__ , __leaf__)) ; + + + + + + +typedef int (*__compar_fn_t) (const void *, const void *); +# 820 "/usr/include/stdlib.h" 3 4 +extern void *bsearch (const void *__key, const void *__base, + size_t __nmemb, size_t __size, __compar_fn_t __compar) + __attribute__ ((__nonnull__ (1, 2, 5))) ; + + +# 1 "/usr/include/x86_64-linux-gnu/bits/stdlib-bsearch.h" 1 3 4 +# 19 "/usr/include/x86_64-linux-gnu/bits/stdlib-bsearch.h" 3 4 +extern __inline __attribute__ ((__gnu_inline__)) void * +bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size, + __compar_fn_t __compar) +{ + size_t __l, __u, __idx; + const void *__p; + int __comparison; + + __l = 0; + __u = __nmemb; + while (__l < __u) + { + __idx = (__l + __u) / 2; + __p = (void *) (((const char *) __base) + (__idx * __size)); + __comparison = (*__compar) (__key, __p); + if (__comparison < 0) + __u = __idx; + else if (__comparison > 0) + __l = __idx + 1; + else + return (void *) __p; + } + + return ((void *)0); +} +# 826 "/usr/include/stdlib.h" 2 3 4 + + + + +extern void qsort (void *__base, size_t __nmemb, size_t __size, + __compar_fn_t __compar) __attribute__ ((__nonnull__ (1, 4))); +# 840 "/usr/include/stdlib.h" 3 4 +extern int abs (int __x) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__const__)) ; +extern long int labs (long int __x) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__const__)) ; + + +__extension__ extern long long int llabs (long long int __x) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__const__)) ; + + + + + + +extern div_t div (int __numer, int __denom) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__const__)) ; +extern ldiv_t ldiv (long int __numer, long int __denom) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__const__)) ; + + +__extension__ extern lldiv_t lldiv (long long int __numer, + long long int __denom) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__const__)) ; +# 872 "/usr/include/stdlib.h" 3 4 +extern char *ecvt (double __value, int __ndigit, int *__restrict __decpt, + int *__restrict __sign) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (3, 4))) ; + + + + +extern char *fcvt (double __value, int __ndigit, int *__restrict __decpt, + int *__restrict __sign) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (3, 4))) ; + + + + +extern char *gcvt (double __value, int __ndigit, char *__buf) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (3))) ; + + + + +extern char *qecvt (long double __value, int __ndigit, + int *__restrict __decpt, int *__restrict __sign) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (3, 4))) ; +extern char *qfcvt (long double __value, int __ndigit, + int *__restrict __decpt, int *__restrict __sign) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (3, 4))) ; +extern char *qgcvt (long double __value, int __ndigit, char *__buf) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (3))) ; + + + + +extern int ecvt_r (double __value, int __ndigit, int *__restrict __decpt, + int *__restrict __sign, char *__restrict __buf, + size_t __len) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (3, 4, 5))); +extern int fcvt_r (double __value, int __ndigit, int *__restrict __decpt, + int *__restrict __sign, char *__restrict __buf, + size_t __len) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (3, 4, 5))); + +extern int qecvt_r (long double __value, int __ndigit, + int *__restrict __decpt, int *__restrict __sign, + char *__restrict __buf, size_t __len) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (3, 4, 5))); +extern int qfcvt_r (long double __value, int __ndigit, + int *__restrict __decpt, int *__restrict __sign, + char *__restrict __buf, size_t __len) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (3, 4, 5))); + + + + + +extern int mblen (const char *__s, size_t __n) __attribute__ ((__nothrow__ , __leaf__)); + + +extern int mbtowc (wchar_t *__restrict __pwc, + const char *__restrict __s, size_t __n) __attribute__ ((__nothrow__ , __leaf__)); + + +extern int wctomb (char *__s, wchar_t __wchar) __attribute__ ((__nothrow__ , __leaf__)); + + + +extern size_t mbstowcs (wchar_t *__restrict __pwcs, + const char *__restrict __s, size_t __n) __attribute__ ((__nothrow__ , __leaf__)) + __attribute__ ((__access__ (__read_only__, 2))); + +extern size_t wcstombs (char *__restrict __s, + const wchar_t *__restrict __pwcs, size_t __n) + __attribute__ ((__nothrow__ , __leaf__)) + __attribute__ ((__access__ (__write_only__, 1, 3))) __attribute__ ((__access__ (__read_only__, 2))); + + + + + + +extern int rpmatch (const char *__response) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))) ; +# 958 "/usr/include/stdlib.h" 3 4 +extern int getsubopt (char **__restrict __optionp, + char *const *__restrict __tokens, + char **__restrict __valuep) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2, 3))) ; +# 1004 "/usr/include/stdlib.h" 3 4 +extern int getloadavg (double __loadavg[], int __nelem) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1))); +# 1014 "/usr/include/stdlib.h" 3 4 +# 1 "/usr/include/x86_64-linux-gnu/bits/stdlib-float.h" 1 3 4 +# 24 "/usr/include/x86_64-linux-gnu/bits/stdlib-float.h" 3 4 +extern __inline __attribute__ ((__gnu_inline__)) double +__attribute__ ((__nothrow__ , __leaf__)) atof (const char *__nptr) +{ + return strtod (__nptr, (char **) ((void *)0)); +} +# 1015 "/usr/include/stdlib.h" 2 3 4 +# 1026 "/usr/include/stdlib.h" 3 4 + +# 3 "code.c" 2 + + + + +# 6 "code.c" +int main() { + int i, n; + n = 0; + + for (i = 0; i < 10; i++){ + n = n + 1; + } + + printf("n = %i\n", n); + + return 0; +} diff --git a/compilaton/code.o b/compilaton/code.o new file mode 100644 index 0000000..ed90532 Binary files /dev/null and b/compilaton/code.o differ diff --git a/compilaton/code.s b/compilaton/code.s new file mode 100644 index 0000000..cfb2d3b --- /dev/null +++ b/compilaton/code.s @@ -0,0 +1,27 @@ + .file "code.c" + .text + .section .rodata.str1.1,"aMS",@progbits,1 +.LC0: + .string "n = %i\n" + .section .text.startup,"ax",@progbits + .p2align 4 + .globl main + .type main, @function +main: +.LFB22: + .cfi_startproc + subq $8, %rsp + .cfi_def_cfa_offset 16 + movl $10, %esi + leaq .LC0(%rip), %rdi + xorl %eax, %eax + call printf@PLT + xorl %eax, %eax + addq $8, %rsp + .cfi_def_cfa_offset 8 + ret + .cfi_endproc +.LFE22: + .size main, .-main + .ident "GCC: (Debian 11.2.0-10) 11.2.0" + .section .note.GNU-stack,"",@progbits diff --git a/mpi/CMakeLists.txt b/mpi/CMakeLists.txt new file mode 100644 index 0000000..dde2924 --- /dev/null +++ b/mpi/CMakeLists.txt @@ -0,0 +1,8 @@ +find_package(MPI REQUIRED COMPONENTS MPICXX) + +foreach(tgt hello_mpi send_recv isend_recv ping_ping ping_pong datatypes pack_unpack) + add_executable(${tgt} ${tgt}.cc) + target_link_libraries(${tgt} PRIVATE ${MPI_CXX_LIBRARIES}) + target_include_directories(${tgt} PRIVATE ${MPI_CXX_INCLUDE_DIRS}) + target_compile_options(${tgt} PRIVATE ${MPI_CXX_COMPILE_OPTIONS}) +endforeach() diff --git a/mpi/datatypes.cc b/mpi/datatypes.cc new file mode 100644 index 0000000..c4fef20 --- /dev/null +++ b/mpi/datatypes.cc @@ -0,0 +1,53 @@ +#include +#include +#include + +int main() { + MPI_Init(NULL, NULL); + + int prank; + MPI_Comm_rank(MPI_COMM_WORLD, &prank); + + MPI_Count lb, extent, size; + + struct Test_t { + double d[2]; + int i; + }; + + std::vector foo(100); + + std::array block_lengths = {2, 1}; + std::array displacements; + std::array old_types = {MPI_DOUBLE, MPI_INT}; + + MPI_Aint addr0, addr1; + MPI_Get_address(&foo[0], &addr0); + MPI_Get_address(&foo[0].d, &displacements[0]); + MPI_Get_address(&foo[0].i, &displacements[1]); + + displacements[0] = MPI_Aint_diff(displacements[0], addr0); + displacements[1] = MPI_Aint_diff(displacements[1], addr0); + + MPI_Datatype mpi_test_t, mpi_test_vector_t; + + MPI_Type_create_struct(2, block_lengths.data(), displacements.data(), + old_types.data(), &mpi_test_t); + + MPI_Get_address(&foo[1], &addr1); + addr1 = MPI_Aint_diff(addr1, addr0); + + MPI_Type_create_resized(mpi_test_t, 0, addr1, &mpi_test_vector_t); + MPI_Type_commit(&mpi_test_vector_t); + + MPI_Type_get_extent_x(mpi_test_t, &lb, &extent); + MPI_Type_size_x(mpi_test_t, &size); + std::cout << "MPI Datatype: mpi_test_t, size: " << size + << " extent: " << extent << std::endl; + std::cout << "C++ Datatype: Test_t, size: " << sizeof(Test_t) << std::endl; + + MPI_Type_free(&mpi_test_vector_t); + MPI_Finalize(); + + return 0; +} diff --git a/mpi/hello_mpi.cc b/mpi/hello_mpi.cc new file mode 100644 index 0000000..ce97df1 --- /dev/null +++ b/mpi/hello_mpi.cc @@ -0,0 +1,15 @@ +#include +#include + +int main(int argc, char *argv[]) { + MPI_Init(&argc, &argv); + + int size, rank; + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + std::cout << "I am process " << rank << " out of " << size << std::endl; + + MPI_Finalize(); + return 0; +} diff --git a/mpi/isend_recv.cc b/mpi/isend_recv.cc new file mode 100644 index 0000000..0959893 --- /dev/null +++ b/mpi/isend_recv.cc @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +void fill_buffer(std::vector &buf) { + for (auto &v : buf) { + v = 0; + } +} + +int main() { + int rank, size; + std::vector buf(100); + + MPI_Init(NULL, NULL); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + + assert(size == 2 && "Works only with 2 procs"); + + MPI_Request request; + if (rank == 0) { + fill_buffer(buf); + MPI_Isend(buf.data(), buf.size(), MPI_INT, 1, 0, MPI_COMM_WORLD, &request); + } else if (rank == 1) { + MPI_Recv(buf.data(), buf.size(), MPI_INT, 0, 0, MPI_COMM_WORLD, + MPI_STATUS_IGNORE); + } + // here I can do computation as long as buf is not modified + MPI_Wait(&request, MPI_STATUS_IGNORE); + + MPI_Finalize(); + + return 0; +} diff --git a/mpi/pack_unpack.cc b/mpi/pack_unpack.cc new file mode 100644 index 0000000..5e3bdb2 --- /dev/null +++ b/mpi/pack_unpack.cc @@ -0,0 +1,50 @@ +#include +#include +#include +#include + +void fill_buffer(std::vector &buf) { + for (auto &v : buf) { + v = 0; + } +} + +int main() { + int rank, size; + std::vector buf(100); + int position{0}, count; + MPI_Status status; + + int a; + double d[10]; + + MPI_Init(NULL, NULL); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + + assert(size == 2 && "Works only with 2 procs"); + + if (rank == 0) { + a = 0xcafe; + MPI_Pack(&a, 1, MPI_INT, buf.data(), buf.size(), &position, MPI_COMM_WORLD); + MPI_Pack(d, 10, MPI_DOUBLE, buf.data(), buf.size(), &position, + MPI_COMM_WORLD); + MPI_Send(buf.data(), position, MPI_PACKED, 1, 0, MPI_COMM_WORLD); + } else if (rank == 1) { + MPI_Recv(buf.data(), buf.size(), MPI_PACKED, 0, 0, MPI_COMM_WORLD, &status); + MPI_Unpack(buf.data(), buf.size(), &position, &a, 1, MPI_INT, + MPI_COMM_WORLD); + MPI_Unpack(buf.data(), buf.size(), &position, d, 10, MPI_DOUBLE, + MPI_COMM_WORLD); + } + + if (rank == 1) { + MPI_Get_count(&status, MPI_PACKED, &count); + std::cout << " position: " << position << " count: " << count << " - a: 0x" + << std::hex << a << std::endl; + } + + MPI_Finalize(); + + return 0; +} diff --git a/mpi/ping_ping.cc b/mpi/ping_ping.cc new file mode 100644 index 0000000..922b4a4 --- /dev/null +++ b/mpi/ping_ping.cc @@ -0,0 +1,57 @@ +#include "print_size.hh" +#include +#include +#include +#include +#include + +void fill_buffer(std::vector &buf, size_t size) { + buf.resize(size); + for (auto &v : buf) { + v = 0; + } +} + +using clk = std::chrono::high_resolution_clock; +using second = std::chrono::duration; + +int main() { + int prank, psize; + const auto N{30}; + size_t REP{1000}; + + MPI_Init(NULL, NULL); + MPI_Comm_rank(MPI_COMM_WORLD, &prank); + MPI_Comm_size(MPI_COMM_WORLD, &psize); + + std::vector buf; + auto partner = (prank + 1) % psize; + + for (size_t n = 0; n < N; n += 2) { + auto size = 1 << n; + fill_buffer(buf, size); + + auto t_start = clk::now(); + for (size_t repetition = 0; repetition < REP; ++repetition) { + MPI_Send(buf.data(), buf.size(), MPI_INT, partner, 0, MPI_COMM_WORLD); + MPI_Recv(buf.data(), buf.size(), MPI_INT, partner, 0, MPI_COMM_WORLD, + MPI_STATUS_IGNORE); + } + auto time_s = (second{clk::now() - t_start}).count() / REP; + + if (prank == 0) { + auto size_b = size * sizeof(int); + std::cout << "PingPing" + << " size: " << std::setw(10) << printHuman(size_b, "B", 2) + << " time: " << std::setw(10) << printHuman(time_s, "s") + << " bandwidth: " << std::setw(10) + << printHuman(size_b / time_s, "B/s", 2) << std::endl; + } + if (size > 256) + REP = 1; + } + + MPI_Finalize(); + + return 0; +} diff --git a/mpi/ping_pong.cc b/mpi/ping_pong.cc new file mode 100644 index 0000000..b538e52 --- /dev/null +++ b/mpi/ping_pong.cc @@ -0,0 +1,65 @@ +#include "print_size.hh" +#include +#include +#include +#include +#include + +void fill_buffer(std::vector &buf, size_t size) { + buf.resize(size); + for (auto &v : buf) { + v = 0; + } +} + +using clk = std::chrono::high_resolution_clock; +using second = std::chrono::duration; + +int main() { + int prank, psize; + const auto N{30}; + size_t REP{1000}; + + MPI_Init(NULL, NULL); + MPI_Comm_rank(MPI_COMM_WORLD, &prank); + MPI_Comm_size(MPI_COMM_WORLD, &psize); + + std::vector buf; + auto partner = (prank + 1) % psize; + + for (size_t n = 0; n < N; ++n) { + auto size = 1 << n; + fill_buffer(buf, size); + + if (size > 256) + REP = 1; + + auto t_start = clk::now(); + for (size_t repetition = 0; repetition < REP; ++repetition) { + if (prank == 0) { + MPI_Send(buf.data(), buf.size(), MPI_INT, partner, 0, MPI_COMM_WORLD); + MPI_Recv(buf.data(), buf.size(), MPI_INT, partner, 0, MPI_COMM_WORLD, + MPI_STATUS_IGNORE); + } else { + MPI_Recv(buf.data(), buf.size(), MPI_INT, partner, 0, MPI_COMM_WORLD, + MPI_STATUS_IGNORE); + MPI_Send(buf.data(), buf.size(), MPI_INT, partner, 0, MPI_COMM_WORLD); + } + } + second time = clk::now() - t_start; + + if (prank == 0) { + auto size_b = size * sizeof(int); + auto time_s = time.count() / REP / 2.; + std::cout << "PingPong" + << " size: " << std::setw(10) << printHuman(size_b, "B", 2) + << " time: " << std::setw(10) << printHuman(time_s, "s") + << " bandwidth: " << std::setw(10) + << printHuman(size_b / time_s, "B/s") << std::endl; + } + } + + MPI_Finalize(); + + return 0; +} diff --git a/mpi/print_size.hh b/mpi/print_size.hh new file mode 100644 index 0000000..3b9335f --- /dev/null +++ b/mpi/print_size.hh @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include +#include + +#ifndef PRINT_SIZE_H_ +#define PRINT_SIZE_H_ + +template +inline std::string printHuman(T size, std::string unit, char base = 10) { + int mult = 0; + int fact = base == 10 ? 3 : 10; + if (size != 0) { + mult = std::floor((std::log(double(size)) / std::log(double(base))) / fact); + } + + auto real_size = double(size) / double(std::pow(base, fact * mult)); + std::array ratio = {"n", u8"μ", "m", "", "K", "M", + "G", "T", "P", "E", "Z", "Y"}; + + std::string base_2_correction; + if (base == 2 and ratio[mult + 3] != "") { + base_2_correction = "i"; + } + + std::stringstream sstr; + sstr << std::fixed << std::setprecision(1) << real_size << ratio[mult + 3] + << base_2_correction << unit; + return sstr.str(); +} + +#endif // PRINT_SIZE_H_ diff --git a/mpi/send_recv.cc b/mpi/send_recv.cc new file mode 100644 index 0000000..35c1d19 --- /dev/null +++ b/mpi/send_recv.cc @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +void fill_buffer(std::vector &buf) { + for (auto &v : buf) { + v = 0; + } +} + +int main() { + int rank, size; + std::vector buf(100); + + MPI_Init(NULL, NULL); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + + assert(size == 2 && "Works only with 2 procs"); + + if (rank == 0) { + fill_buffer(buf); + MPI_Ssend(buf.data(), buf.size(), MPI_INT, 1, 0, MPI_COMM_WORLD); + } else if (rank == 1) { + MPI_Recv(buf.data(), buf.size(), MPI_INT, 0, 0, MPI_COMM_WORLD, + MPI_STATUS_IGNORE); + } + + MPI_Finalize(); + + return 0; +} diff --git a/mpi/sendrecv.cc b/mpi/sendrecv.cc new file mode 100644 index 0000000..d6c4912 --- /dev/null +++ b/mpi/sendrecv.cc @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +void fill_buffer(std::vector &buf, rank) { + for (auto &v : buf) { + v = 0; + } +} + +int main() { + int rank, size; + std::vector buf(100); + + MPI_Init(NULL, NULL); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + + assert(size == 2 && "Works only with 2 procs"); + + fill_buffer(buf, rank); + MPI_Sendrecv(buf.data(), buf.size(), MPI_INT, 1, 0, buf.data(), buf.size(), + MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + + MPI_Finalize(); + + return 0; +} diff --git a/openmp/CMakeLists.txt b/openmp/CMakeLists.txt new file mode 100644 index 0000000..781ada9 --- /dev/null +++ b/openmp/CMakeLists.txt @@ -0,0 +1,8 @@ +find_package(OpenMP REQUIRED COMPONENTS CXX) + +foreach(tgt hello hello_cond sections private for dgemm) + add_executable(${tgt} ${tgt}.cc) + target_link_libraries(${tgt} OpenMP::OpenMP_CXX) +endforeach() + +add_executable(hello_cond_wo_openmp hello_cond.cc) diff --git a/openmp/dgemm.cc b/openmp/dgemm.cc new file mode 100644 index 0000000..b302e9d --- /dev/null +++ b/openmp/dgemm.cc @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include + +using clk = std::chrono::high_resolution_clock; +using second = std::chrono::duration; +using time_point = std::chrono::time_point; + +int main() { + int N = 2000; + std::vector A(N * N), B(N * N), C(N * N); + + for (auto &a : A) + a = 1.0; + + for (auto &b : B) + b = 2.0; + + for (auto &c : C) + c = 0.0; + + auto verif = [&](auto &&A) { + double sum = 0.; + for (auto &a : A) + sum += a; + return sum; + }; + + auto nthreads = omp_get_max_threads(); + + auto t1 = clk::now(); +#pragma omp parallel for collapse(1) schedule(static, N / nthreads) + for (int i = 0; i < N; ++i) + for (int j = 0; j < N; ++j) + for (int k = 0; k < N; ++k) + C[i * N + j] += A[i * N + k] * B[k * N + j]; + + second elapsed = clk::now() - t1; + + std::printf("DGEMM with %i threads, collapse(1): %g GFLOP/s (verif %g)\n", + nthreads, ((2. * N * N * N) / elapsed.count() / 1e8), + verif(C) / (1. * N * N * N)); + + return 0; +} diff --git a/openmp/for.cc b/openmp/for.cc new file mode 100644 index 0000000..07b53d4 --- /dev/null +++ b/openmp/for.cc @@ -0,0 +1,16 @@ +#include +#include + +int main() { + +#pragma omp parallel num_threads(2) + { + auto myrank = omp_get_thread_num(); +#pragma omp for + for (int i = 0; i < 6; ++i) { + std::printf("Thread %i handling i=%i\n", myrank, i); + } + } + + return 0; +} diff --git a/openmp/hello.cc b/openmp/hello.cc new file mode 100644 index 0000000..a139e6d --- /dev/null +++ b/openmp/hello.cc @@ -0,0 +1,14 @@ +#include +#include + +int main() { + +#pragma omp parallel + { + auto mysize = omp_get_num_threads(); + auto myrank = omp_get_thread_num(); + std::printf("Hello from thread %i out of %i\n", myrank, mysize); + } + + return 0; +} diff --git a/openmp/hello_cond.cc b/openmp/hello_cond.cc new file mode 100644 index 0000000..6a38884 --- /dev/null +++ b/openmp/hello_cond.cc @@ -0,0 +1,21 @@ +#include +#if defined(_OPENMP) +# include +#endif + +int main() { + int mysize = 1; + int myrank = 0; + +#if defined(_OPENMP) +#pragma omp parallel + { + mysize = omp_get_num_threads(); + myrank = omp_get_thread_num(); +#endif + std::printf("Hello from thread %i out of %i\n", myrank, mysize); +#if defined(_OPENMP) + } +#endif + return 0; +} diff --git a/openmp/private.cc b/openmp/private.cc new file mode 100644 index 0000000..bb0285e --- /dev/null +++ b/openmp/private.cc @@ -0,0 +1,22 @@ +#include +#include + +int main() { + int a = 1, b = 2; + double c = 3.; + + std::printf("Thread %i sees, a, b, c: %i, %i, %g (before)\n", + omp_get_thread_num(), a, b, c); + +#pragma omp parallel num_threads(3), private(a), firstprivate(b) + { + std::printf("Thread %i sees, a, b, c: %i, %i, %g (inside)\n", + omp_get_thread_num(), a, b, c); + c = -1e-3; + } + + std::printf("Thread %i sees, a, b, c: %i, %i, %g (after)\n", + omp_get_thread_num(), a, b, c); + + return 0; +} diff --git a/openmp/sections.cc b/openmp/sections.cc new file mode 100644 index 0000000..e35ac47 --- /dev/null +++ b/openmp/sections.cc @@ -0,0 +1,17 @@ +#include +#include + +int main() { + +#pragma omp parallel sections num_threads(4) + { +#pragma omp section + std::printf("Thread %i handling section 1\n", omp_get_thread_num()); +#pragma omp section + std::printf("Thread %i handling section 2\n", omp_get_thread_num()); +#pragma omp section + std::printf("Thread %i handling section 3\n", omp_get_thread_num()); + } + + return 0; +} diff --git a/optimization/CMakeLists.txt b/optimization/CMakeLists.txt new file mode 100644 index 0000000..88677d5 --- /dev/null +++ b/optimization/CMakeLists.txt @@ -0,0 +1,3 @@ +foreach(tgt daxpy) + add_executable(${tgt} ${tgt}.cc) +endforeach() diff --git a/optimization/daxpy.cc b/optimization/daxpy.cc new file mode 100644 index 0000000..26185d7 --- /dev/null +++ b/optimization/daxpy.cc @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +using clk = std::chrono::high_resolution_clock; +using millisecond = std::chrono::duration; + +int main() { + const int N = 1e8; + + std::vector a(N), b(N), c(N); + + for (int i = 0; i < N; ++i) { + a[i] = 1.0 * i; + b[i] = 10.0 * i; + } + + std::mt19937 gen(0); + std::uniform_real_distribution<> rand(0, 1000); + + auto alpha = rand(gen); + + auto t1 = clk::now(); + for (int i = 0; i < N; ++i) { + c[i] = a[i] + alpha * b[i]; + } + auto t2 = clk::now(); + + millisecond ms_double = t2 - t1; + + std::cout << "For loop ran in " << ms_double.count() << "ms" << std::endl; + return 0; +}