c:pthreads:parallel-increment-vector
                Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| c:pthreads:parallel-increment-vector [2024/01/16 21:55] – 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; | ||
| } | } | ||
c/pthreads/parallel-increment-vector.1705442120.txt.gz · Last modified:  (external edit)
                
                