开发者

Computing Sums Using Multithreading

开发者 https://www.devze.com 2023-04-04 23:33 出处:网络
I am working on a program that is to take the input of 15,000 integers from a file. After reading the values, the thread should then create 10 threads with each thread responsible for computing the su

I am working on a program that is to take the input of 15,000 integers from a file. After reading the values, the thread should then create 10 threads with each thread responsible for computing the sum of their block (1,500 values each). Each thread will then print the sum of its values, and the main thread will compute the sum from all 10 threads.

What I have is mind is to read in all values and store them in an int array while using an int to keep count of the number of values read (let's call it int values). I would then divide this number by the number of threads I would like to determine the number of values per block that each thread should have (let's call it int block). I would then start a thread, loop through the array (int block times) while incrementing the array index count, and then start a new thread as long as the array index count does not equal the last array index.

Is this the right way of looking at this problem? Is there a simpler approach? We have been given hints to utilize pthread_create, pthread_join, pthread_exit, pthread_attr_init, pthread_attr_destroy, and pthread_setdetachstate. This is my first attempt at multithreading, so it would be great to get feedback on where in my code I should initiate and end each thread so that it is actually multithreading and not performing an individual thread multiple times. Any help would be greatly appreciated!

EDIT: Stuck on command line arguments

#include <stdio.h>

int main(int argc, char *argv[]) {
int i;
FILE *fp;
int c;

for (i = 1; i < argc; i++) {
    fp = fopen(argv[i], "r");

    if (fp == NULL) {
        fprint(stderr, "cat: can't open %s\n", argv[i]);
        continue;
    }

    while ((c = getc(fp)) != EOF) {
  开发者_如何学Go      putchar(c);
    }

    fclose(fp);
}

return 0;
}

I seem to have forgotten how terrible I was at I/O when we covered this section. What are the command line arguments to test my program (prob_5.c) with a given parameter?


Your plan sounds good. If I were you, I'd try to execute it and come back with specific problems should you encounter any.

it would be great to get feedback on where in my code I should initiate and end each thread

Your main thread will be creating the workers. This would be part of the loop that you describe in some detail. In all likelihood the workers will terminate by returning from their thread function.

One thing that you shouldn't be expecting is speed-up. In all probability your ten-worker version will be slower than its single-threaded equivalent. This has to do with the small size of the input array and the overhead of spawning threads and subsequent synchronization. Besides, as rightly pointed out by @Adam Rosenfield in the comments, the overall program is likely to be I/O-bound anyway.


Some things to think about:

1) How will your main thread know that the summing operations are complete and data available from all the concurrently-running threads? With some designs, including yours, the last thread you start may not be the last to finish.

2) How might you time it to see if there is any speedup? With 15000 integers, a stopwatch is not gong to be any use!

3) You may want to mention that, if this summing operation is likely to be used as part of a larger app and wll be run more than once, (or run concurrently on multiple input files), creating 10 threads at startup and having them wait for summing requests on a producer-consumer queue, (so creating a thread pool), will improve overall performance by eliminating continual thread creation/termination. You could pool up some IntBlocks as well, but there is not much point in such optimizations here because:

4) As already noted here, 15000 integers is not very much data and summing is a fast operation, so note the warnings from the other posters about slow disk I/O etc.

5) When you get your homework app going, it might be interesting to see how it performs with an SSD - maybe you can get your tutor/prof to buy you one

Rgds, Martin

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号