- Admin
- #1
Today, I spent an hour or*two rewriting an old program I wrote two months ago to run faster and more efficiently. The sole purpose of this program is to start at*x = 3 and iterate until x = n (with some optimizations that I am neglecting to mention), checking to see if 2^x - 1 is prime. I wrote this program to get better at C and I am rewriting it because I want to learn how to do multithreading. The test to check if a number of the form 2^x*- 1 is prime is fairly intensive and can take a long time with large numbers of n, so this would be an ideal situation to do multithreading.
Since I am using my university's Unix server for my classes, I want to use pthreads.h to multithread my program, to get a head start on a concurrency course I will be taking two years from now.
There are two models that I have identified which I could use to multithread my application. I am simplifying them by assuming that my program will create only two threads.
One model*involves starting two threads for each test, waiting for each of them to end and then starting two more threads, using the main thread to tell each new thread what they are supposed to do, until all of the tests have been performed.
The other model involves starting two threads that will loop through the tests, knowing not to do a test that has either already been done or is being done by another thread and having the main thread wait until they are both done. I think I can accomplish this if I put some variables into some sort of structure that will tell each thread what has not been done, so that when a thread finishes a test, it will take the next test, modifying the variables to indicate what*the next unfinished test and then proceed to do the new test. I would also like the thread to be able to lock the structure, so that if the other thread happens to finish while it is using it, it will have to wait until the first thread has finished. I believe such a structure is called a mutex, although I have only read about them on Wikipedia and I have never done multithreading, excluding one crazy attempt I made at multithreading in high school, that if I recall involved the first approach (using a P4C with hyperthreading) and was not appreciably better than single threading.
I imagine that with the first approach, there will be many times where one thread will finish before the other thread, leaving one core doing nothing while the other core finishes its work. I want my program to scale well as the number of cores increases and I do not think the first model does that very well, so I would like to use the second model, which I believe should keep all of the cores busy virtually at all times, given how long the tests take for large exponents (at exponents of 1 million, I believe they can take days)*and and how little time each thread will need to interact with the mutex.
There are two things that I need help with, the first being fairly essential and the second being something that will make my life much easier.
The first is figuring how out to use pthreads to do what I want. Assuming that the functions in pthreads behave like the functions evildictator used in his code, my main dilemma is*figuring out how to*create the mutex, initialize the mutex so that it has the variables my threads will need,*how the threads will modify the mutex and how and when and where*the mutex will be destroyed (does the*main thread destroy it?):
http://channel9.msdn.com/ShowPost.aspx?PostID=361764
I am doing this in C89 (ANSI C) with the*pthreads*library (and the GMP library, but not on the server), so I will not be using any C++ or any other libraries.
My program is meant to use the GMP library, but I cannot get GMP to work on my university's server and any attempt to install it puts my user account over its quota, limiting the life of the GMP install (whether it works or not) to seven days, so I am right now using a fork (I think that is the correct jargon)*that modifies my program to not use GMP*on the server, which limits*the fork*to 32bit numbers, as the tests need twice the number of bits the numbers tested use. An irony to this is that while I cannot get GMP working on the server, I can get it working on my home computer with Visual Studio 2005 Standard Edition.
This leads to my second problem, which is that if I multithread my program on the server, I will never get to see my program working as it is supposed to work (i.e. it will*not*run for any*appreciable amount of time)*unless I also multithread it on my home computer as well. I will be using Unix's multithreading libraries*in my classes two years from now, so I want to avoid using any methods of multithreading that are not avaliable on Unix. I did some research and the best solution to this problem appears to be using win32-pthreads:
http://sourceware.org/pthreads-win32/
Unfortunately, I do not know how to get the library working with Visual Studio. I managed to get GMP working by doing a google search on my problem and finding a thread where someone suggested using absolute paths. Given that googling*win32-pthreads only gives 1160 results, I doubt the same problem solving method would work. I have tried putting the*pthread.h*from*win32-pthread into my project and then using #include , but that did not work and I have not taken any classes that involve using custom libraries, much less any using Visual Studio, so I am not sure how to proceed.
If anyone could help me with these two problems, I would be very grateful.
More...
View All Our Microsft Related Feeds
Since I am using my university's Unix server for my classes, I want to use pthreads.h to multithread my program, to get a head start on a concurrency course I will be taking two years from now.
There are two models that I have identified which I could use to multithread my application. I am simplifying them by assuming that my program will create only two threads.
One model*involves starting two threads for each test, waiting for each of them to end and then starting two more threads, using the main thread to tell each new thread what they are supposed to do, until all of the tests have been performed.
The other model involves starting two threads that will loop through the tests, knowing not to do a test that has either already been done or is being done by another thread and having the main thread wait until they are both done. I think I can accomplish this if I put some variables into some sort of structure that will tell each thread what has not been done, so that when a thread finishes a test, it will take the next test, modifying the variables to indicate what*the next unfinished test and then proceed to do the new test. I would also like the thread to be able to lock the structure, so that if the other thread happens to finish while it is using it, it will have to wait until the first thread has finished. I believe such a structure is called a mutex, although I have only read about them on Wikipedia and I have never done multithreading, excluding one crazy attempt I made at multithreading in high school, that if I recall involved the first approach (using a P4C with hyperthreading) and was not appreciably better than single threading.
I imagine that with the first approach, there will be many times where one thread will finish before the other thread, leaving one core doing nothing while the other core finishes its work. I want my program to scale well as the number of cores increases and I do not think the first model does that very well, so I would like to use the second model, which I believe should keep all of the cores busy virtually at all times, given how long the tests take for large exponents (at exponents of 1 million, I believe they can take days)*and and how little time each thread will need to interact with the mutex.
There are two things that I need help with, the first being fairly essential and the second being something that will make my life much easier.
The first is figuring how out to use pthreads to do what I want. Assuming that the functions in pthreads behave like the functions evildictator used in his code, my main dilemma is*figuring out how to*create the mutex, initialize the mutex so that it has the variables my threads will need,*how the threads will modify the mutex and how and when and where*the mutex will be destroyed (does the*main thread destroy it?):
http://channel9.msdn.com/ShowPost.aspx?PostID=361764
I am doing this in C89 (ANSI C) with the*pthreads*library (and the GMP library, but not on the server), so I will not be using any C++ or any other libraries.
My program is meant to use the GMP library, but I cannot get GMP to work on my university's server and any attempt to install it puts my user account over its quota, limiting the life of the GMP install (whether it works or not) to seven days, so I am right now using a fork (I think that is the correct jargon)*that modifies my program to not use GMP*on the server, which limits*the fork*to 32bit numbers, as the tests need twice the number of bits the numbers tested use. An irony to this is that while I cannot get GMP working on the server, I can get it working on my home computer with Visual Studio 2005 Standard Edition.
This leads to my second problem, which is that if I multithread my program on the server, I will never get to see my program working as it is supposed to work (i.e. it will*not*run for any*appreciable amount of time)*unless I also multithread it on my home computer as well. I will be using Unix's multithreading libraries*in my classes two years from now, so I want to avoid using any methods of multithreading that are not avaliable on Unix. I did some research and the best solution to this problem appears to be using win32-pthreads:
http://sourceware.org/pthreads-win32/
Unfortunately, I do not know how to get the library working with Visual Studio. I managed to get GMP working by doing a google search on my problem and finding a thread where someone suggested using absolute paths. Given that googling*win32-pthreads only gives 1160 results, I doubt the same problem solving method would work. I have tried putting the*pthread.h*from*win32-pthread into my project and then using #include , but that did not work and I have not taken any classes that involve using custom libraries, much less any using Visual Studio, so I am not sure how to proceed.
If anyone could help me with these two problems, I would be very grateful.
More...
View All Our Microsft Related Feeds