Skip to content

W11: Home Study

Atanbori edited this page Jan 15, 2025 · 2 revisions

CMP9133 - Workshop 11

University of Lincoln School of Engineering and Physical Sciences CMP9133 – Programming Principles

Task 1: Implement a multi-threaded program to calculate the factorial of a given number.

Instruction:

  1. Implement a function calculateFactorial() that takes an integer as input and calculates its factorial recursively.
  2. Divide the workload of calculating the factorial across multiple threads to make use of parallelism, by using the method calculatePartialFactorial().
  3. Create multiple threads, each responsible for calculating the factorial of a subset of the number's range.
  4. In the calculateFactorial() function, join the threads and collect the results.

355133098-353ea7cd-68a1-4f9c-9e02-1125cd011901

By implementing this task, you will gain hands-on experience in the following multi-threading concepts:

  • Creating and managing threads.
  • Dividing workload across multiple threads.
  • Synchronization and thread safety (to avoid race conditions).
  • Joining threads and collecting results.

Task 2: Implement a multithreaded program to parallelize matrix multiplication.

Instructions:

  1. Create functions to initialize the matrices with random values and display them.
  2. Implement a function multiplyMatrices() that performs matrix multiplication.
  3. Divide the workload of matrix multiplication across multiple threads to achieve parallelism, by using the method multiplyMatricesPart().
  4. In multiplyMatrices(), create multiple threads, each responsible for calculating a subset of the resulting matrix.
  5. Don't forget to join the threads and collect the results.

355133486-8b878f1b-9fb9-4344-8596-b175a24bb033