Skip to content

Commit e3517a4

Browse files
author
pap0018
committed
First commit
0 parents  commit e3517a4

36 files changed

+1675
-0
lines changed

.gitignore

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
lib
2+
*~
3+
*.o
4+
*.swp
5+
*#
6+
Object files
7+
*.o
8+
*.ko
9+
*.obj
10+
*.elf
11+
12+
# Precompiled Headers
13+
*.gch
14+
*.pch
15+
16+
# Libraries
17+
*.lib
18+
*.a
19+
*.la
20+
*.lo
21+
22+
# Shared objects (inc. Windows DLLs)
23+
*.dll
24+
*.so
25+
*.so.*
26+
*.dylib
27+
28+
# Executables
29+
*.exe
30+
*.out
31+
*.app
32+
*.i*86
33+
*.x86_64
34+
*.hex
35+
*-omp
36+
*-gomp
37+
*-asm
38+
*-domp
39+
*-dgomp
40+
*.prv
41+
*.pcf
42+
*.row
43+
*.mpits
44+
set-0
45+
*.sym

src/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CC = gcc
2+
3+
CFLAGS = -Wall -O -std=c99 -march=native -I. -D_GNU_SOURCE
4+
CFLAGS_SO = -fPIC --shared
5+
6+
TARGETS = libminiomp.so
7+
all: $(TARGETS)
8+
9+
libminiomp.so: libminiomp.h libminiomp.c env.c intrinsic.c parallel.c synchronization.c single.c loop.c task.c taskloop.c
10+
$(CC) $(CFLAGS_SO) $(CFLAGS) $+ -o $(MINIOMP)/lib/$@ -lpthread
11+
12+
clean:
13+
rm -rf $(MINIOMP)/lib/$(TARGETS)

src/env.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <unistd.h>
2+
#include "libminiomp.h"
3+
4+
// Global variable storing the ICV (internal control variables) supported in our implementation
5+
miniomp_icv_t miniomp_icv;
6+
7+
// Function to parse OpenMP environment variables, currently just OMP_NUM_THREADS
8+
void parse_env(void) {
9+
char * env = getenv ("OMP_NUM_THREADS");
10+
if (env == NULL) {
11+
printf("Environment variable OMP_NUM_THREADS not defined, setting nthreads_var ICV to number of cores\n");
12+
int procs = (int)sysconf( _SC_NPROCESSORS_ONLN ); // returns the number of cores in system
13+
if (procs < 0)
14+
miniomp_icv.nthreads_var = 1;
15+
else miniomp_icv.nthreads_var = procs;
16+
} else {
17+
miniomp_icv.nthreads_var = atoi(env);
18+
}
19+
printf("Setting nthreads_var ICV to %d\n", miniomp_icv.nthreads_var);
20+
}

src/env.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Type declaration for Internal Control Variables (ICV) structure
2+
typedef struct {
3+
int nthreads_var;
4+
// Add other ICVs if needed
5+
} miniomp_icv_t;
6+
7+
// Global variable storing the ICV (internal control variables) supported in our implementation
8+
extern miniomp_icv_t miniomp_icv;
9+
10+
// Functions implemented in this module
11+
void parse_env(void);

src/intrinsic.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "libminiomp.h"
2+
3+
void omp_set_num_threads (int n) {
4+
miniomp_icv.nthreads_var = (n > 0 ? n : 1);
5+
}
6+
7+
int omp_get_num_threads (void) {
8+
return(miniomp_icv.nthreads_var);
9+
}
10+
11+
int omp_get_thread_num (void) {
12+
//printf("TBI: omp_get_thread_num still doesn't know who I am ... let's say I am 0\n");
13+
int thread_id = ((miniomp_parallel_t*) (pthread_getspecific(miniomp_specifickey)))->id;
14+
return(thread_id);
15+
}
16+
17+
// No need to implement this function, it is just involked by Extrae at some point
18+
// and returns the current nesting for parallel regions
19+
int omp_get_level (void) {
20+
printf("TBI: omp_get_level ... let say current nesting level is 1\n");
21+
return(1);
22+
}
23+

src/intrinsic.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// OpenMP intrinsics
2+
3+
// Sets the number of threads to be used in the following parallel regions
4+
void omp_set_num_threads (int n);
5+
6+
// Returns the number of threads in the current parallel region
7+
int omp_get_num_threads (void);
8+
9+
// Returns the identifier of the invoking thread within the team
10+
int omp_get_thread_num (void);
11+
12+
// This function returns the nesting level for the parallel region,
13+
// which enclose the calling call
14+
int omp_get_level (void);

src/libminiomp.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "libminiomp.h"
2+
//#include "intrinsic.h"
3+
4+
// Library constructor and desctructor
5+
void init_miniomp(void) __attribute__((constructor));
6+
void fini_miniomp(void) __attribute__((destructor));
7+
8+
// Function to parse OMP_NUM_THREADS environment variable
9+
void parse_env(void);
10+
11+
void
12+
init_miniomp(void) {
13+
printf ("mini-omp is being initialized\n");
14+
// Parse OMP_NUM_THREADS environment variable to initialize nthreads_var internal control variable
15+
parse_env();
16+
// Initialize Pthread data structures
17+
// Initialize Pthread thread-specific data, useful for example to store the OpenMP thread identifier
18+
pthread_key_create(&miniomp_specifickey, NULL);
19+
// Initialize OpenMP default lock and default barrier
20+
// Initialize OpenMP workdescriptors for for and single
21+
// Initialize OpenMP task queue for task and taskloop
22+
}
23+
24+
void
25+
fini_miniomp(void) {
26+
// free structures allocated during library initialization
27+
printf ("mini-omp is finalized\n");
28+
}

src/libminiomp.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <ctype.h>
4+
#include <stdbool.h>
5+
#include <string.h>
6+
#include <stdint.h>
7+
#include <pthread.h>
8+
9+
// Maximum number of threads to be supported by our implementation
10+
// To be used whenever you need to dimension thread-related structures
11+
#define MAX_THREADS 32
12+
13+
extern pthread_key_t miniomp_specifickey;
14+
15+
// To implement memory barrier (flush)
16+
//void __atomic_thread_fence(int);
17+
#define mb() __atomic_thread_fence(3)
18+
19+
#include "intrinsic.h"
20+
#include "env.h"
21+
#include "parallel.h"
22+
#include "synchronization.h"
23+
#include "loop.h"
24+
#include "single.h"
25+
#include "task.h"

src/loop.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "libminiomp.h"
2+
3+
// Declaratiuon of global variable for loop work descriptor
4+
miniomp_loop_t miniomp_loop;
5+
6+
/* The *_next routines are called when the thread completes processing of
7+
the iteration block currently assigned to it. If the work-share
8+
construct is bound directly to a parallel construct, then the iteration
9+
bounds may have been set up before the parallel. In which case, this
10+
may be the first iteration for the thread.
11+
12+
Returns true if there is work remaining to be performed; *ISTART and
13+
*IEND are filled with a new iteration block. Returns false if all work
14+
has been assigned. */
15+
16+
bool
17+
GOMP_loop_dynamic_next (long *istart, long *iend) {
18+
printf("TBI: Asking for more iterations? I gave you all at the beginning, no more left ...\n");
19+
return(false);
20+
}
21+
22+
/* The *_start routines are called when first encountering a loop construct
23+
that is not bound directly to a parallel construct. The first thread
24+
that arrives will create the work-share construct; subsequent threads
25+
will see the construct exists and allocate work from it.
26+
27+
START, END, INCR are the bounds of the loop; CHUNK_SIZE is the
28+
scheduling parameter.
29+
30+
Returns true if there's any work for this thread to perform. If so,
31+
*ISTART and *IEND are filled with the bounds of the iteration block
32+
allocated to this thread. Returns false if all work was assigned to
33+
other threads prior to this thread's arrival. */
34+
35+
bool
36+
GOMP_loop_dynamic_start (long start, long end, long incr, long chunk_size,
37+
long *istart, long *iend)
38+
{
39+
printf("TBI: What a mess! Starting a non-static for worksharing construct and dont know what to do, I'll take it all\n");
40+
*istart = start;
41+
*iend = end;
42+
return(true);
43+
}
44+
45+
/* The GOMP_loop_end* routines are called after the thread is told that
46+
all loop iterations are complete. The first version synchronize
47+
all threads; the nowait version does not. */
48+
49+
void
50+
GOMP_loop_end (void) {
51+
printf("TBI: Finishing a for worksharing construct with non static schedule\n");
52+
}
53+
54+
void
55+
GOMP_loop_end_nowait (void) {
56+
printf("TBI: Finishing a for worksharing construct with non static schedule, with nowait clause\n");
57+
}
58+
59+
#if 0
60+
// Only implement this if really needed, i.e. you find a case in which it is invoked
61+
62+
/* The GOMP_parallel_loop_* routines pre-initialize a work-share construct
63+
to avoid one synchronization once we get into the loop. The compiler
64+
does not invoke the *_start routine for the work-share. And of course,
65+
the compiler does not invoke GOMP_loop_end. These routines should create
66+
the team of threads to execute the work-share in parallel */
67+
68+
void
69+
GOMP_parallel_loop_dynamic (void (*fn) (void *), void *data,
70+
unsigned num_threads, long start, long end,
71+
long incr, long chunk_size, unsigned flags)
72+
{
73+
printf("TBI: What another mess! Directly starting a parallel and a non-static for worksharing construct! I am lost!\n");
74+
// Here you should pre-initialize the work-sharing structures as done in
75+
// GOMP_loop_dynamic_start; only the master thread is doing this, the other
76+
// threads in the team do not reach this point
77+
GOMP_parallel (fn, data, num_threads, flags);
78+
}
79+
#endif

src/loop.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Type declaration for loop worksharing descriptor
2+
typedef struct {
3+
long start; // loop bounds and increment
4+
long end;
5+
long incr;
6+
7+
int schedule; // schedule kind for loop
8+
long chunk_size;
9+
// complete the definition of worksharing descriptor
10+
} miniomp_loop_t;
11+
12+
#define ws_STATIC 0
13+
#define ws_STATICCHUNK 1
14+
#define ws_DYNAMIC 2
15+
#define ws_GUIDED 3
16+
#define ws_RUNTIME 4
17+
#define ws_AUTO 5
18+
19+
extern miniomp_loop_t miniomp_loop;

0 commit comments

Comments
 (0)