Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

I need some simple assignment to demonstrate parallelism using some synchnronization…

  1. User

    I need some simple assignment to demonstrate parallelism using some synchnronization mechanism, something simple but representative, examples that we should take as inspiration are matrice multiplication, pi approximation, training of ML mode… (please create something other then example)

  2. Assistant

    Certainly! For demonstrating parallelism with synchronization, let's design a simple assignment where multiple threads will work together to perform a calculation on an array of numbers. In this case, we will calculate the cumulative sum of a large array, where each thread is responsible for a portion of the array and then contributes to the final sum, ensuring proper synchronization to avoid race conditions.

    Assignment: Parallel Cumulative Sum Calculation

    Objective:

    To calculate the cumulative sum of an array of integers in parallel using threads and a synchronization mechanism to correctly manage access to the shared total sum.

    Description:

    You will implement a parallel algorithm to calculate the cumulative sum of a large array of integers. Each thread will be responsible for summing a sub-section of the array and then update a global sum variable. The threads must properly synchronize their access to this global sum to prevent race conditions.

    Requirements:

    1. Divide the work among threads: Partition the array into sub-arrays, with each thread handling the sum of one sub-array.
    2. Synchronization mechanism: Use a mutex, semaphore, or monitor (depending on what is available in your chosen programming language) to synchronize access to the global sum variable.
    3. Main Thread: The main thread should create the worker threads, start them, and wait for them to complete.
    4. Reporting: Each thread should print its local sum before adding to the global sum. After all threads have completed, the main thread should print the global cumulative sum.

    Example Pseudo-code:

    INIT array[N] with random integers
    DEFINE globalSum as an integer
    DEFINE mutex or another synchronization tool
    
    FUNCTION calculateLocalSum(startIndex, endIndex):
        localSum = 0
        FOR i FROM startIndex TO endIndex:
            localSum += array[i]
        END FOR
        PRINT "Local Sum by Thread-X: " + localSum
        LOCK mutex
        globalSum += localSum
        UNLOCK mutex
    
    MAIN:
        INIT threads[T]
        chunkSize = N / T
        FOR i FROM 0 TO T-1:
            startIndex = i * chunkSize
            endIndex = (i+1) * chunkSize
            threads[i] = CREATE_THREAD(calculateLocalSum, startIndex, endIndex)
        END FOR
        FOR i FROM 0 TO T-1:
            JOIN threads[i] (wait for threads to complete)
        END FOR
        PRINT "Global Cumulative Sum: " + globalSum
    

    This assignment will allow students to experience multiple core aspects of parallel programming:

    • Dividing a task into portions that can be worked on simultaneously
    • Encountering and resolving race conditions using synchronization mechanisms
    • Coordinating threads to aggregate the final result

    Instructions will, of course, change depending on the programming language used (e.g., C++, Python, Java). Students should be encouraged to experiment with different numbers of threads and array sizes to observe the effects on performance and to ensure their synchronization method effectively prevents race conditions.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.