Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add two multiprocess commands #1

Open
wants to merge 1 commit into
base: rocket-chip-vcu128
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package/Config.in
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,11 @@ menu "Miscellaneous"
source "package/xutil_util-macros/Config.in"
endmenu

menu "MULTIPROCESS Packages"
source "package/pth_hello/Config.in"
source "package/thread_sum/Config.in"
endmenu

menu "Networking applications"
source "package/aircrack-ng/Config.in"
source "package/alfred/Config.in"
Expand Down
5 changes: 5 additions & 0 deletions package/pth_hello/Config.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
config BR2_PACKAGE_PTH_HELLO
bool "pth_hello"
help
pth_hello package.

19 changes: 19 additions & 0 deletions package/pth_hello/pth_hello.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
################################################################################
#
# pth_hello package
#
################################################################################

PTH_HELLO_VERSION = 1.0
PTH_HELLO_SITE = package/pth_hello/src
PTH_HELLO_SITE_METHOD = local# Other methods like git,wget,scp,file etc. are also available.

define PTH_HELLO_BUILD_CMDS
$(MAKE) CC="$(TARGET_CC)" LD="$(TARGET_LD)" -C $(@D)
endef

define PTH_HELLO_INSTALL_TARGET_CMDS
$(INSTALL) -D -m 0755 $(@D)/pth_hello $(TARGET_DIR)/usr/bin
endef

$(eval $(generic-package))
8 changes: 8 additions & 0 deletions package/pth_hello/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: clean
.PHONY: pth_hello

pth_hello: pth_hello.c
$(CC) -o '$@' '$<' -lpthread

clean:
-rm pth_hello
39 changes: 39 additions & 0 deletions package/pth_hello/src/pth_hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

/* Global variable: accessible to all threads */
int thread_count;

void* Hello(void* rank); // Thread function

int main(int argc, char* argv[]){
long thread;
pthread_t* thread_handles;

/* Get number of threads from command line */
thread_count = strtol(argv[1], NULL, 10);

thread_handles = malloc(thread_count*sizeof(pthread_t));

for (thread = 0; thread < thread_count; thread++)
pthread_create(&thread_handles[thread], NULL,
Hello, (void*) thread);

printf("Hello from the main thread\n");

for (thread = 0; thread < thread_count; thread++)
pthread_join(thread_handles[thread], NULL);

free(thread_handles);

return 0;
} /* main */

void* Hello(void* rank){
long my_rank = (long) rank;

printf("Hello from thread %ld of %d\n", my_rank, thread_count);

return NULL;
} /* Hello */
5 changes: 5 additions & 0 deletions package/thread_sum/Config.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
config BR2_PACKAGE_THREAD_SUM
bool "thread_sum"
help
thread_sum package.

8 changes: 8 additions & 0 deletions package/thread_sum/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: clean
.PHONY: thread_sum

thread_sum: thread_sum.c
$(CC) -o '$@' '$<' -lpthread

clean:
-rm thread_sum
65 changes: 65 additions & 0 deletions package/thread_sum/src/thread_sum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

/* Global variable: accessible to all threads */
int thread_count;
long long n;
double sum;
pthread_mutex_t mutex;

void* Thread_sum(void* rank); // Thread function

int main(int argc, char* argv[]){
long thread;
pthread_t* thread_handles;

pthread_mutex_init( &mutex, NULL );

/* Get number of threads from command line */
thread_count = strtol(argv[1], NULL, 10);

thread_handles = malloc(thread_count*sizeof(pthread_t));

printf("Thread_sum from the main thread\n");
for ( n = 1e5; n <= 1e10; n *= 10, sum = 0 ) {
for (thread = 0; thread < thread_count; thread++)
pthread_create(&thread_handles[thread], NULL,
Thread_sum, (void*) thread);

for (thread = 0; thread < thread_count; thread++)
pthread_join(thread_handles[thread], NULL);
printf("pi = %.11f @ n = %lld\n", 4.0*sum, n);
}

pthread_mutex_destroy( &mutex );

free(thread_handles);

return 0;
} /* main */

void* Thread_sum(void* rank){
long my_rank = (long) rank;

double factor;
long long i;
long long my_n = n/thread_count;
long long my_first_i = my_n * my_rank;
long long my_last_i = my_first_i + my_n;
double my_sum = 0.0;

if (my_first_i % 2 == 0)
factor = 1.0;
else
factor = -1.0;

for (i = my_first_i; i < my_last_i; i++, factor = -factor)
my_sum += factor/(2*i+1);

pthread_mutex_lock(&mutex);
sum += my_sum;
pthread_mutex_unlock(&mutex);

return NULL;
} /* Thread_sum */
19 changes: 19 additions & 0 deletions package/thread_sum/thread_sum.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
################################################################################
#
# thread_sum package
#
################################################################################

THREAD_SUM_VERSION = 1.0
THREAD_SUM_SITE = package/thread_sum/src
THREAD_SUM_SITE_METHOD = local# Other methods like git,wget,scp,file etc. are also available.

define THREAD_SUM_BUILD_CMDS
$(MAKE) CC="$(TARGET_CC)" LD="$(TARGET_LD)" -C $(@D)
endef

define THREAD_SUM_INSTALL_TARGET_CMDS
$(INSTALL) -D -m 0755 $(@D)/thread_sum $(TARGET_DIR)/usr/bin
endef

$(eval $(generic-package))