llnl / hpc-tutorials Goto Github PK
View Code? Open in Web Editor NEWFuture home of hpc-tutorials.llnl.gov
Home Page: https://hpc-tutorials.llnl.gov
License: MIT License
Future home of hpc-tutorials.llnl.gov
Home Page: https://hpc-tutorials.llnl.gov
License: MIT License
As from the title, in PThreads tutorial, Passing Arguments to Threads, Example 3
Example 3 - Thread Argument Passing (Incorrect)
This example performs argument passing incorrectly. It passes the address of variable t, which is shared memory space and visible to all threads. As the loop iterates, the value of this memory location changes, possibly before the created threads can access it.
int rc;
long t;
for(t=0; t<NUM_THREADS; t++)
{
printf("Creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &t);
...
}
with output
Creating thread 0
Creating thread 1
...
Creating thread 7
Hello from thread 140737488348392
Hello from thread 140737488348392
...
Hello from thread 140737488348392
Hello from thread 140737488348392
It looks like the program prints the address of the variable t
, which might be inconsistent with what we want to illustrate --- "the value of this memory location changes". Can we adjust this to something like the following?
void *PrintHello(void *threadid) {
long * tid;
tid = (long *)threadid;
printf("Hello World! It's me, thread #%ld!\n", *tid);
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int rc;
long t;
for (t = 0; t < NUM_THREADS; t++)
{
printf("In main: creating thread %ld\n", t);
/* Don't try to access memory location of t!
* It constantly changes as the loop goes!
* Possibly before the created threads can access it
*/
rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &t);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
/* Last thing that main() should do */
pthread_exit(NULL);
return 0;
}
So the output goes like
In main: creating thread 0
In main: creating thread 1
Hello World! It's me, thread #1!
In main: creating thread 2
Hello World! It's me, thread #2!
In main: creating thread 3
Hello World! It's me, thread #3!
In main: creating thread 4
Hello World! It's me, thread #4!
Hello World! It's me, thread #5!
which illustrates that the value t
had been t++
ed before the created thread read the location.
Thanks!
Hello,
I come from the https://hpc-tutorials.llnl.gov/posix/
It seems that styles on this page are broken because of issues with https://icsmaint-ext.llnl.gov/ site which is throwing 503s.
Is it something on my side only?
Best regards,
Furai
May wish to update https://github.com/LLNL/HPC-Tutorials/blob/main/mpi/routine_args.md to have more Fortran data types. See for example https://www.mpich.org/static/docs/v4.0/www3/Constants.html
Error:
If you go to these two pages, only the first one will let you follow the links to routine-specific info.
https://github.com/LLNL/HPC-Tutorials/blob/main/mpi/appendix_a.md
https://hpc-tutorials.llnl.gov/mpi/appendix_a/
Cause:
The links are relative to the current directory, but because the live server serves appendix_a.md as a directory in the url, the links end up with a bonus /appendix_a/
in the path, which results in a 404.
Example:
Github links correctly: https://github.com/LLNL/HPC-Tutorials/blob/main/mpi/MPI_appendix/MPI_Bcast.txt
Live site links to this 404: https://hpc-tutorials.llnl.gov/mpi/appendix_a/MPI_appendix/MPI_Bcast.txt
Live site has the page here: https://hpc-tutorials.llnl.gov/mpi/MPI_appendix/MPI_Bcast.txt
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.