c:pthreads:parallel-increment-vector
                Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| c:pthreads:parallel-increment-vector [2024/01/16 21:51] – created odefta | c:pthreads:parallel-increment-vector [2025/01/02 18:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| #include < | #include < | ||
| - | #define NUM_THREADS 4 // Numărul de thread-uri | + | #define NUM_THREADS 4 // Number of threads to use | 
| - | #define VECTOR_SIZE 100 // Dimensiunea vectorului | + | #define VECTOR_SIZE 100 // Size of the array | 
| - | // Structura pentru a stoca informațiile pentru fiecare thread | + | // Structure to pass information to threads | 
| typedef struct { | typedef struct { | ||
| - |     int start;  | + |     int start;  | 
| - | int end; // Indexul de sfârșit pentru acest thread | + | int end; // Ending index for this thread | 
| - |     int *vector;  | + |     int *vector;  | 
| } ThreadData; | } ThreadData; | ||
| - | // Funcția executată de fiecare  | + | // Function that each thread  | 
| void *increment_vector(void *arg) { | void *increment_vector(void *arg) { | ||
| ThreadData *data = (ThreadData *)arg; | ThreadData *data = (ThreadData *)arg; | ||
| + | // Increment the array elements in the specified range | ||
|     for (int i = data-> |     for (int i = data-> | ||
|         data-> |         data-> | ||
| Line 35: | Line 36: | ||
|     int segment_size = VECTOR_SIZE / NUM_THREADS; |     int segment_size = VECTOR_SIZE / NUM_THREADS; | ||
| - | // Inițializează vectorul | + | // Initialize the array | 
|     for (int i = 0; i < VECTOR_SIZE; |     for (int i = 0; i < VECTOR_SIZE; | ||
| vector[i] = i; | vector[i] = i; | ||
| } | } | ||
| - | // Creează thread-urile | + | // Create threads and assign them segments of the array | 
|     for (int i = 0; i < NUM_THREADS; |     for (int i = 0; i < NUM_THREADS; | ||
|         thread_data[i].start = i * segment_size; |         thread_data[i].start = i * segment_size; | ||
| Line 49: | Line 50: | ||
| } | } | ||
| - | // Așteaptă ca thread-urile să termine | + | // Wait for all threads to complete | 
|     for (int i = 0; i < NUM_THREADS; |     for (int i = 0; i < NUM_THREADS; | ||
|         pthread_join(threads[i], |         pthread_join(threads[i], | ||
| } | } | ||
| - | // Afișează vectorul incrementat | + | // Display the incremented array | 
|     for (int i = 0; i < VECTOR_SIZE; |     for (int i = 0; i < VECTOR_SIZE; | ||
|         printf(" |         printf(" | ||
| } | } | ||
| + | |||
| return 0; | return 0; | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | The process involves splitting the array into nearly equal segments and assigning each segment to a different thread for incrementing. \\ | ||
| + | |||
| + | **Define an Array and Thread Information**: | ||
| + | |||
| + | **Thread Function**: Write a function that each thread will execute. This function should accept a range of indices to process (start and end indices) and increment the elements within this range. | ||
| + | |||
| + | **Create and Initialize Threads**: Create threads and pass them their respective array segments to process. Ensure the segments cover the entire array and are as evenly distributed as possible. | ||
| + | |||
| + | ** Wait for Threads to Complete**: After starting all threads, use pthread_join to wait for all threads to finish their execution. | ||
| + | |||
| + | **Finalize**: | ||
| + | |||
| + | This method efficiently utilizes multiple cores (if available) to perform the increment operation in parallel, potentially reducing the total processing time compared to a single-threaded approach. It's important in such parallel operations to carefully manage thread creation and synchronization to avoid issues like race conditions. | ||
| + | |||
| + | |||
| + | |||
| + | |||
c/pthreads/parallel-increment-vector.1705441912.txt.gz · Last modified:  (external edit)
                
                