How to "multithread" c code

     
I have a number crunching application written in C. It is kind of a main loop that for each value calls, for increasing values of "i", a function that performs some calculations. I read about multithreading, và I am considering learning a bit about it, in C. I wonder if somehow general code lượt thích mine could be automatically multithreaded & how.

Bạn đang xem: How to "multithread" c code

Thanks

P.D. Lớn get an idea about my code, let"s say that it is something lượt thích this:

main(...)for(i=0;i...

void get_result(float x,float y,float result) result=sqrt(log (x) + log (y) + cos (exp (x + y));(and some more similar mathematical operations)
c multithreading
tóm tắt
Improve this question
Follow
edited Oct 11, 2010 at 17:59
xuất hiện the way
asked Oct 11, 2010 at 16:19

*

mở cửa the wayOpen the way
24k4747 gold badges138138 silver badges193193 bronze badges
4
showroom a bình luận |

13 Answers 13


Sorted by: Reset to mặc định
Highest score (default) Date modified (newest first) Date created (oldest first)
26
If the task is highly parallelizable and your compiler is modern, you could try OpenMP. Http://en.wikipedia.org/wiki/OpenMP


chia sẻ
Improve this answer
Follow
answered Oct 11, 2010 at 16:21
*

NovikovNovikov
4,33122 gold badges2525 silver badges3636 bronze badges
4
showroom a comment |
23
One alternative lớn multithread your code would be using pthreads ( provides more precise control than OpenMP ).

Assuming x, y & result are global variable arrays,

#include ...void *get_result(void *param) // param is a dummy pointer...int main(){...pthread_t *tid = malloc( ntimes * sizeof(pthread_t) );for( i=0; i(Compile your code with gcc prog.c -lpthread)


giới thiệu
Improve this answer
Follow
answered Oct 16, 2010 at 17:25
user191776user191776
2
địa chỉ cửa hàng a phản hồi |
12
+25
You should have a look at openMP for this. The C/C++ example on this page is similar lớn your code:https://computing.llnl.gov/tutorials/openMP/#SECTIONS

#include #define N 1000main (){int i;float a, b, c, d;/* Some initializations */for (i=0; i If you prefer not to lớn use openMP you could use either pthreads or clone/wait directly.

Xem thêm: Điểm Chuẩn Học Viện Biên Phòng Điểm Chuẩn 2019 : Điểm Chuẩn Học Viện Biên Phòng

No matter which route you choose you are just dividing up your arrays into chunks which each thread will process. If all of your processing is purely computational (as suggested by your example function) then you should vị well lớn have only as many threads as you have logical processors.

There is some overhead with adding threads to bởi vì parallel processing, so make sure that you give each thread enough work to biến hóa for it. Usually you will, but if each thread only ends up with 1 computation lớn do và the computations aren"t that difficult to vị then you may actually slow things down. You can always have fewer threads than you have processors if that is the case.

Xem thêm: Bảng Đạo Hàm Của Các Công Thức Tính Đạo Hàm Cơ Bản Và Nâng Cao Đầy Đủ Nhất

If you bởi have some IO going on in your work then you may find that having more threads than processors is a win because while one thread may be blocking waiting for some IO lớn complete another thread can be doing its computations. You have lớn be careful doing IO to lớn the same file in threads, though.