English
Review tasks
Practical tasks by course topic to check your knowledge. Each task is a separate program.
Topic 1. Fundamentals of parallel computing
- Create a console program that prompts for the fraction of parallel code (from 0 to 1) and the number of processors, validates the input, and prints the speedup and efficiency according to Amdahl’s law and the speedup limit.
- Create a console program that prompts for the sequential fraction of a program and the maximum number of processors and prints a table of the scaled speedup and efficiency according to the Gustafson–Barsis law for
up to the given value. - Create a console program that reads “number of processors – measured time” pairs from the keyboard (they must include a measurement for one processor) and prints a table of the speedup, efficiency, and Karp–Flatt metric.
- Create a console program that prompts for the desired speedup and the fraction of parallel code and, using Amdahl’s law, determines the minimum number of processors needed for this speedup or reports that it is unattainable.
- Create a console program that, from the measured speedup on
processors (entered by the user), computes the fraction of parallel code according to Amdahl’s law and predicts the speedup for and processors. - Create a console program that uses
Stopwatchto measure the time to compute the sum of the squares of the numbers from 1 to ( is entered), performs a warmup and 7 runs, and prints the minimum, median, and maximum time. - Create a console program that measures the time of three stages of processing an array of random numbers of a user-specified size (filling, sorting, computing the sum), and prints the share of each stage and the speedup predicted by Amdahl’s law for 8 cores if the sorting is parallelized.
- Create a console program that prints a report on the computer’s hardware and software configuration: the operating system, architecture, .NET version, number of logical processors, available memory, build configuration, and timer resolution.
- Create a console program that prompts for the execution time of a task on one processor, the fraction of parallel code, and the number of processors, and prints the predicted time, speedup, efficiency, and cost (processor time) according to Amdahl’s law.
- Create a console program that compares the time to traverse an
two-dimensional array ( is entered) by rows and by columns (the median of 5 runs with a warmup) and prints how many times faster one way is than the other.
Topic 2. Processes and threads
- Create a console program that prompts for the number of threads N (1 to 32), starts N named
Threadthreads, each of which computes the sum of the squares of the numbers from 1 to 1,000,000·(thread number) and writes the result to its own array element, and afterJoinprints a “thread name – ManagedThreadId – result” table. - Create a console program that prompts for the array size and the number of threads, fills the array with random numbers (fixed seed), computes the sum of the array by partitioning it among N threads and sequentially, checks that the results match, and prints the time of both variants, the speedup, and the efficiency.
- Create a console program that queues 50 jobs in the thread pool (computing the factorial of a number from 1 to 50 as a
BigInteger), waits for all jobs to finish, and prints the results in order of their numbers and the number of distinct pool threads that executed them. - Create a console program that starts a background thread that increments a counter every 100 ms until a
volatile boolflag is set; after Enter is pressed, the main thread sets the flag, waits at most 1 s for the thread to finish, and prints the counter value and whether the thread finished in time. - Create a console program that starts a thread that converts strings from an array to numbers; an exception in the thread is caught, stored, and, after
Join, printed by the main thread together with the number of the invalid string and the sum of the valid numbers. - Create a coordinator console program that prompts for the number of processes N, starts itself N times with the arguments
--worker i N(a child process counts the primes in its part of the range up to 1,000,000), reads the standard output of each process, checks the exit codes, and prints the total number of primes and the time. - Create a console program that prints the affinity mask and the priority class of its own process, prompts for a new mask in binary, validates it, sets the mask and the
BelowNormalclass, performs a computation in 4 threads, and prints the time before and after changing the mask. - Create a console program that uses
System.Threading.Timerto print the current time every N milliseconds (N is entered, 100 to 5,000), stops the timer after Enter is pressed, and prints the number of ticks and the IDs of the pool threads in which the callback ran. - Create a console program that prompts for the upper bound of a range and the maximum number of threads, counts the primes by partitioning among 1, 2, 4, … threads, and prints a “threads – time – speedup – efficiency” table with the median of three runs and a check of the result.
- Create a console program that, in three threads, uses
ThreadLocal<Random>with different seeds to generate 1,000,000 random numbers per thread and aThreadLocal<int>counter withtrackAllValues, and after completion prints the average of each thread’s numbers and the values of all counters.
Topic 3. Thread synchronization
- Create a console program that prompts for the number of threads and the number of increments per thread, increments a shared counter without synchronization, with
Interlocked.Increment, and withlock, and prints a “method – value obtained – expected value – time, ms” table. - Create a console program that prompts for the number of threads and a range of integers, divides the range among the threads, and finds the maximum digit sum of a number, updating a shared maximum with an
Interlocked.CompareExchangeCAS loop. Print the maximum, the number at which it is reached, and a check against a sequential computation. - Create a console program with a bank account class in which the
DepositandWithdrawmethods (without going negative) are protected by a separateLockobject. The program prompts for the number of threads and operations, performs random operations, and checks that the final balance equals the initial balance plus the sum of the successful operations. - Create a console program with a bounded buffer on
Monitor.WaitandMonitor.PulseAll. The user enters the buffer capacity and the number of producers, consumers, and items; the program prints the number of items produced and consumed, their sums, and the maximum buffer occupancy. - Create a console program that models a parcel pickup point: it prompts for the number of windows and customers, limits simultaneous service with a
SemaphoreSlimwith a waiting timeout, and prints an event log, the number of refusals, and the maximum number of customers served simultaneously. - Create a console program with a product catalog protected by
ReaderWriterLockSlim. The user enters the number of readers, the number of requests, and the share of writes in percent; the program compares the running time withlockand withReaderWriterLockSlimand prints the number of reads and writes performed. - Create a console program in which the user enters the number of worker threads. The workers start simultaneously on a
ManualResetEventSlimsignal and perform work of random duration, and the main thread waits for them to finish through aCountdownEvent. Print the start and finish time of each worker. - Create a console program that models transfers between accounts in several threads with a naive lock order and detects a deadlock by a
Jointimeout, and then performs the same transfers with the locks ordered by account number. Print the result of both variants and a check of the total amount. - Create a console program that solves the dining philosophers problem for a number of philosophers entered by the user, taking the forks with
Monitor.TryEnterwith a timeout and a random delay. Print the number of meals and retries of each philosopher. - Create a console program that prompts for the number of participants and the number of stages of a competition. At each stage, the participant threads prepare for different lengths of time and start simultaneously through a
Barrier; the program prints each participant’s time at each stage and a final standings table.
Topic 4. Thread-safe collections
- Create a console program that prompts for the number of threads (1–32) and the number of random numbers, counts in several threads how many times each last digit occurs in a shared
ConcurrentDictionary<int, long>with theAddOrUpdatemethod, and prints a “digit – count” table and the result of a check against a sequential count. - Create a console program that prompts for the number of producers, consumers, and items, implements the producer–consumer pattern on a
BlockingCollection<int>with bounded capacity, correctly stops all consumers after all producers finish, and prints the number and sum of each consumer’s items with a check of the total sum. - Create a console program that prompts for the number of lines and the channel capacity, builds a two-stage pipeline on bounded channels (generating lines of numbers → computing the sum of a line in two tasks → totaling), and prints the total sum, the number of lines, and the time, checking the sum against a sequential computation.
- Create a console program that prompts for the number of threads and operations, puts jobs into and takes them from a
ConcurrentQueue<int>in several threads using only theEnqueueandTryDequeuemethods, and prints the number of jobs added, removed, and remaining, with a check that no job was processed twice. - Create a console program that prompts for the number of threads and keys, has several threads simultaneously request the value of an “expensive” function for the keys through a
ConcurrentDictionary<int, Lazy<long>>, and prints the values, the running time, and the number of function calls, which must equal the number of keys. - Create a console program that prompts for the number of threads and numbers, counts the even numbers of an array in several threads in two ways (counters in a shared array and local variables), performs a warmup and the median of three runs, and prints a time and speedup table for 1, 2, 4, and 8 threads with an explanation of the difference.
- Create a console program in which several threads add a keyboard-entered number of strings to a shared
ImmutableList<string>throughImmutableInterlocked.Update, after which the program prints the number of elements and the number of transformation retries and checks that the list contains all the strings. - Create a console program that prompts for the channel capacity and the overflow mode (
DropOldest,DropNewest,DropWrite), quickly writes 10,000 numbers with theTryWritemethod to a bounded channel with a slow consumer, and prints the number of numbers written, read, and dropped, counted by theitemDroppeddelegate. - Create a console program that prompts for the number of consumers and jobs, adds the jobs to a
BlockingCollection<string?>with “poison pills” (null) to stop each consumer, and prints how many jobs each consumer processed and that all threads finished. - Create a console program with a nonblocking maximum counter: several threads (their number is entered) generate random numbers and update a shared maximum with an
Interlocked.CompareExchangeCAS loop. The program prints the maximum found and the number of failed CAS attempts and checks the maximum with a sequential pass.
Topic 5. TPL tasks and async/await
- Create a console program that prompts the user for the number of tasks N (1–16) and an upper bound M, divides the range 1…M into N parts, counts the primes in each
Task.Runtask, and afterTask.WhenAllprints the number of primes in each part, the total count, and the computation time, and checks the result against a sequential count. - Create a console program that simultaneously starts three simulated requests to weather services with different random delays, prints the first response received using
Task.WhenAny, cancels the remaining requests with a token, and prints the states of all tasks. - Create a console program that builds a chain of tasks “read a file → count the words → write the result to a file” with
ContinueWith, adds a continuation withOnlyOnFaultedto print an error, and checks the behavior for an existing and a missing file whose path the user enters. - Create a console program that starts 5 tasks, two of which throw exceptions of different types, awaits them with
Task.WhenAllin atryblock, and prints the first exception caught byawait, all exceptions from theExceptionproperty, and the state of each task. - Create a console program that computes the sum of the series 1/k² for k from 1 to 2 billion in a task with a cancellation token, checking the token every 1,000,000 iterations. The user enters a timeout in seconds (
CancelAfter); the program prints either the result or a cancellation message with the number of completed iterations and the state of the task. - Create a console program that asynchronously copies a file whose path the user enters in 1 MB blocks, prints the progress in percent through its own implementation of
IProgress<int>, and allows the copying to be canceled with the Esc key, deleting the incomplete copy. - Create a console program that asynchronously reads all text files of a given folder (
File.ReadAllTextAsync) at once, no more than 4 files at a time (SemaphoreSlim), and prints a “file – lines – words” table with a total row and the overall time. - Create a console program that starts a local
HttpListenertest server with three pages (one responds with a 3 s delay) and, using a singleHttpClientinstance, downloads the pages simultaneously with a 1 s timeout per request, printing for each page its size or a timeout message. - Create a console program with an asynchronous
IAsyncEnumerable<int>generator that returns a random number from 1 to 100 every 200 ms (PeriodicTimer). The program iterates over the numbers with anawait foreachloop, prints them and the running average, and stops after a number of seconds entered by the user through a cancellation token. - Create a console program with a simulator class that raises a
Completedevent after a random time from 0.5 to 3 s. UsingTaskCompletionSource, the program turns the event into a task, awaits it with a 2 s timeout (WaitAsync), and prints the result of waiting for 5 runs.
Topic 6. Data parallelism and PLINQ
- Create a console program that prompts for the array size
(from 10⁶ to 10⁸), fills the array with random integers from 0 to 999 (fixed seed), and computes the sum of the squares of the elements sequentially and withParallel.Forwith thread-local state (localInit,localFinally). The program checks that the sums match and prints the time of both ways and the speedup. - Create a console program that, for an array of
floating-point numbers (the user enters the size), computes the mean and variance withParallel.ForEachwithPartitioner.Create(0, n, rangeSize), where the range size is also entered by the user, checks the result against a sequential computation, and prints the number of ranges, the result, and the time. - Create a console program that generates an array of
string records “name;city;age” (fixed seed) and, using PLINQ (AsParallel,Where,GroupBy), computes for each city the number of people older than an age entered by the user. The result is printed sorted by city name, together with the PLINQ and LINQ times and a check that the results match. - Create a console program that uses PLINQ
Aggregatewith seed, update, combine, and result functions to compute simultaneously, for an array of random integers (the user enters the size), the minimum, maximum, sum, and number of even numbers. The program checks the result against a sequential computation and prints it and the time. - Create a console program that finds, in an array of 50,000,000 random integers, the index of the first element equal to a number entered by the user using
Parallel.ForwithParallelLoopState.Break. The program printsIsCompleted,LowestBreakIteration, the result of a sequential search, and the time of both searches, or a message that the number is absent. - Create a console program that sorts an array of
random integers (the user enters the size and the threshold) with a parallel merge sort: the two halves are sorted throughParallel.Invoke, and fragments smaller than the threshold are sorted withArray.Sort. The program checks that the result is sorted and prints the time of the parallel sort, the sequential merge sort, and the speedup. - Create a console program that estimates the number π by the Monte Carlo method for a number of trials entered by the user, dividing the trials into 64 blocks with their own generators
new Random(seed + block number)inParallel.For. The program prints the estimate, the error, and a table of time, speedup, and efficiency for 1, 2, 4, and 8 threads. - Create a console program that finds the maximum of an array of
random numbers ( from 10 to 26 is entered by the user) with a parallel reduction: at each level of the tree, the pairwise maxima are computed throughParallel.Forinto a new array half as long. The program prints the maximum and the number of levels and checks the result withEnumerable.Max. - Create a console program that processes 100 simulated “files” (each is a
Task.Delaydelay of 100 to 500 ms and the computation of a sum of random numbers) usingParallel.ForEachAsyncwith a limit on the number of concurrent operations entered by the user and a cancellation token that fires after a given number of seconds. The program prints the number of files processed, the total sum, and the time, or a cancellation message. - Create a console program that computes the Mandelbrot set for an image whose dimensions the user enters, using
Parallel.Forover rows withParallelOptions.MaxDegreeOfParallelismfor 1, 2, 4, 8, and 16 threads. For each thread count, the program prints the median of three runs, the speedup, and the efficiency, and checks that the total number of iterations matches the sequential version.
Topic 7. SIMD vectorization
- Create a console program that prints the values of
Vector.IsHardwareAccelerated,Vector<float>.Count,IsHardwareAcceleratedforVector128,Vector256,Vector512, andIsSupportedforAvx2,Fma,Avx512F,AdvSimdin an aligned table, as well as the widest accelerated vector on this PC. - Create a console program that prompts for an array length (0 to 10,000,000), fills an
intarray with random numbers, computes the sum in scalar code and withVector<int>with tail handling, checks that the sums match, and prints the time of both ways. - Create a console program that multiplies a
floatarray by a number entered by the user and adds it to another array (theaxpyoperation) in scalar code and withVector256<float>with tail handling, checks the result for lengths 0, 1, 7, 8, 9, and 1,000,003, and prints the time. - Create a console program that, in a
shortarray of 10,000,000 samples, replaces all values greater than an entered threshold with the threshold, in scalar code and vectorized (Vector256.GreaterThanandConditionalSelect), and prints the number of changed values and the time of both ways. - Create a console program that finds, in a text file whose path the user enters, the positions of all occurrences of an entered ASCII character using
Vector128.EqualsandExtractMostSignificantBits, prints the count and the first 10 positions, and checks the result with a scalar search. - Create a console program that generates 100,000 vectors of dimension 128, finds for an entered vector number the 5 most similar vectors by cosine similarity (
TensorPrimitives.CosineSimilarity), and prints a “number – similarity” table and the time. - Create a console program that prompts for the matrix size
and the tile size, multipliesdoublematrices in ikj order and in blocks, checks that the results match, and prints the time and GFLOPS of both ways. - Create a console program that solves a diagonally dominant linear system of a size entered by the user by the Jacobi method with
Parallel.Forover rows to a tolerance of , and prints the number of iterations, the maximum solution error, and the time of the sequential and parallel versions. - Create a console program that solves a linear system of size
(entered by the user) by Gaussian elimination with partial pivoting, performing row elimination throughParallel.ForandTensorPrimitives.MultiplyAdd, and prints the residual and the time. - Create a console program that normalizes 1,000,000
Vector3vectors with theVector3.Normalizemethod and vectorized over separate coordinate arrays (Vector256<float>,Vector256.Sqrt), checks the maximum difference, and prints the time and speedup.
Topic 8. Parallel algorithms
- Create a console program that prompts for the integration limits and the tolerance and computes
with the parallel composite Simpson rule, doubling the number of segments until the error estimate by Runge’s rule becomes smaller than the given tolerance; it prints the value, the number of segments, the error estimate, and the time. - Create a console program that computes
with the adaptive Simpson method with recursiveParallel.Invoketasks down to a depth entered by the user, and prints the value, the error relative to , the number of tasks, and the time compared with the sequential version. - Create a console program that prompts for the vector length and the number of threads and computes the dot product with the block, cyclic, and block-cyclic (blocks of 1024) distributions, printing the results with 17 digits and the time of each distribution.
- Create a console program that prompts for the matrix size
and the number of threads, multiplies a random matrix by a vector with horizontal stripes, checks the result against sequential multiplication, and prints the time, speedup, and efficiency. - Create a console program that prompts for the matrix size
(a multiple of 4) and multiplies the matrices with a checkerboard scheme (16 tasks, each computing its own block ), checks the result against sequential multiplication, and prints the time of both ways and the speedup. - Create a console program that finds all roots of the function
on a segment entered by the user: it isolates the roots in parallel on a grid with a given number of segments and refines them by bisection to ; it prints the roots and the time. - Create a console program that, for an entered number of initial velocities, solves the equation of a damped pendulum by the fourth-order Runge–Kutta method until the pendulum stops, in parallel with dynamic distribution of tasks, and prints the number of revolutions for every tenth velocity and the time.
- Create a console program that multiplies
matrices (entered by the user) with stripes on 1, 2, 4, and 8 threads, measures the time, and prints a “threads – predicted – measured – efficiency” table. - Create a console program that estimates the volume of a ball of radius 1 by the Monte Carlo method for an entered number of points with a separate generator for each of 64 blocks, and prints the estimate, the confidence interval, and a check that two runs give the same result.
- Create a console program that solves the tridiagonal system
of size (entered by the user, from a known solution) by the conjugate gradient method with parallel deterministic dot products, and prints the number of iterations, the residual, and the time of the sequential and parallel versions.
Topic 9. Multithreading in C++
- Create a CMake project (the
Threads::Threadstarget, the C++23 standard, debug and release presets with the Ninja generator) with a C++ console program that prompts for the array size and the number of threads, computes the sum of the squares of the elements instd::jthreadthreads, and prints the sum, the time, and the speedup compared with a sequential computation. - Create a C++ console program that prompts for
and the number of threads , counts the primes up to by dividing the range into parts forstd::jthreadthreads, checks the result against a sequential count, and prints a table of time, speedup, and efficiency for from 1 to the entered value. - Create a C++ console program in which 8 threads perform an entered number of transfers among 5 accounts with
std::scoped_lock, and a separate counter of successful transfers is implemented withstd::atomic. The program checks that the total amount has not changed and prints the balances and the number of successful and rejected transfers. - Create a C++ console program with a thread-safe queue on
std::mutexandstd::condition_variablein which 2 producers generate an entered number of jobs and 3 consumers process them (computing a factorial modulo ). The program stops the consumers correctly and prints the number of jobs processed by each consumer and the sum of the results. - Create a C++ console program that, for an entered directory, starts a
std::async(std::launch::async, …)task for each subdirectory that counts the number and size of the.cppand.hfiles usingstd::filesystem, and prints a table of the subdirectories, the overall totals, and error messages received through thefuture. - Create a C++ console program with a thread pool on
std::jthread,std::condition_variable, andstd::packaged_taskthat accepts tasks computing Fibonacci numbers modulo a number for user-entered indices and returns the results throughstd::future; the program prints the results in the order of input and the number of pool threads. - Create a C++ console program that generates an entered number of random floating-point numbers, sorts them with
std::sortwith theseqandparpolicies, and computes the root mean square withstd::transform_reducewith theseq,par, andpar_unseqpolicies. The program prints a table of time, speedup, and a check that the results are identical. - Create a C++ console program that starts 4
std::jthreadworker threads that compute a sum of random numbers until a stop is requested throughstd::stop_token, and the main thread stops them after a number of seconds entered by the user. The program prints the number of numbers processed and the sum of each thread. - Create a C++ console program in which 10 visitor threads use a reading room with 3 seats limited by a
std::counting_semaphore, and a simultaneous start of all visitors is provided by astd::latch. The program prints an event log and checks that there were never more than 3 visitors in the room. - Create a C++ console program that computes the integral of the function
on an entered segment with the trapezoidal rule in threads, measures the time with thestd::chrono::steady_clockclock as the median of three runs, and prints a table of , , , and the value of the integral.
Topic 10. OpenMP
- Create a C++ console program with OpenMP that prompts for the number of threads (1 to 64) and in a parallel region prints for each thread its number, the number of threads in the team, and a private variable initialized through
firstprivatewith a value entered by the user, increased by the thread number; after the region, the program prints the value of this variable and explains why it did not change. - Create a C++ console program with OpenMP that prompts for the number of segments
and computes the number as the integral of on with theparallel for reduction(+:sum)directive for 1, 2, 4, 8, and 16 threads, printing a table of the value, the error, the time (omp_get_wtime), the speedup, and the efficiency. - Create a C++ console program with OpenMP that prompts for the upper limit
and counts the primes up to withschedule(static),schedule(dynamic, 100), andschedule(guided), checks that the number of primes is the same, and prints a time table for each schedule kind. - Create a C++ console program with OpenMP that generates an array of
random integers ( is entered by the user, fixed seed) and finds in parallel the sum, the minimum, the maximum, and the number of even numbers withreductionclauses, checks the results with a sequential pass, and prints them. - Create a C++ console program with OpenMP that generates
student grades from 0 to 100 and builds a histogram of 10 intervals in three ways (critical,atomic,reduction(+:hist[0:10])), checks that the histograms are identical, and prints the histogram and the time of each way. - Create a C++ console program with OpenMP that prompts for
(20 to 45) and computes the -th Fibonacci number recursively withtaskandtaskwaittasks with a threshold below which the computation is sequential; the program prints the result and the time for thresholds of 10, 20, and 30. - Create a C++ console program with OpenMP that sorts an array of
random numbers ( is entered by the user) with a recursive merge sort with OpenMP tasks and a threshold of 10,000 elements, checks that it is sorted, and prints the time of the parallel sort and ofstd::sort. - Create a C++ console program with OpenMP that computes the dot product of two vectors of
floating-point numbers ( is entered by the user) in four ways: a plain loop,omp simd reduction,parallel for reduction, andparallel for simd reduction, checks the relative discrepancy of the results (at most ), and prints a time and speedup table. - Create a C++ console program with OpenMP that prints the value of
omp_get_proc_bind, the number of placesomp_get_num_places, and the processors of each place, and in a parallel region the thread number and the number of its placeomp_get_place_num. Run the program with the variablesOMP_PLACES=coresandOMP_PROC_BIND=closeandspread, and explain the difference in the output. - Create a C++ console program with OpenMP that prompts for the size
of square matrices and multiplies two random matrices with theparallel fordirective over rows for 1, 2, 4, 8, and 16 threads, checks that the result matches sequential multiplication, and prints a table of time, speedup , and efficiency .
Topic 11. GPU computing
- Create a CUDA C++ console program that prompts for the length of two vectors, fills them with random numbers, computes their element-wise product with a kernel that uses a global index and a bounds check, checks the result on the CPU, and prints the largest error.
- Create a CUDA C++ console program that allocates device memory for an array of
numbers ( is entered by the user), copies the data to the GPU, multiplies each element by 2 with a kernel, copies the result back, frees the memory, and checks all CUDA calls. - Create a CUDA C++ console program that inverts a generated 1920×1080 grayscale image with a kernel on a 2D grid of 16×16 blocks, writes the result to a PGM, and prints the grid dimensions.
- Create a CUDA C++ console program that transposes an
matrix ( is entered by the user) with a naive kernel and with a kernel with a 32×32 tile in shared memory, checks the results, and prints the time of both kernels measured with CUDA events. - Create a CUDA C++ console program that computes the sum of an array of
integers with a reduction with sequential addressing in shared memory andatomicAdd, and compares the result and the time with OpenMP. - Create a CUDA C++ console program that builds a histogram of 10 intervals for
random grades from 0 to 100 with local histograms in shared memory and prints the histogram and the time. - Create a CUDA C++ console program that, for an array of
numbers, measures separately with CUDA events the time of copying to the GPU, of the kernel (the square of each element), and of copying back, and prints the speedup relative to OpenMP with and without the copies. - Create a C# console program with ILGPU that prints the list of available devices, selects a GPU (or a CPU accelerator), and computes SAXPY for
numbers with a kernel withIndex1DandMemoryBuffer1D, checking the result. - Create a C# console program with ILGPU that blurs a generated image with a 3×3 mean kernel with
Index2Dand compares the time withParallel.For, taking the copies into account. - Create a CUDA C++ console program that processes an array in parts in four CUDA streams with pinned memory (copy, kernel, copy) and compares the time with processing without streams.
Topic 12. MPI message passing
- Create a C++ console program with MPI in which each process prints its rank, the number of processes, and the node name, and rank 0 additionally gathers the ranks of all processes with the
MPI_Gatheroperation and prints them in ascending order. - Create a C++ console program with MPI that computes the sum of the numbers from 1 to
( is entered by the user on rank 0): rank 0 broadcasts with theMPI_Bcastoperation, each rank sums its part, andMPI_Reducecollects the result, which is compared with the formula. - Create a C++ console program with MPI in which the ranks form a ring and pass a token (a number) around it, each rank adding its rank to it; the exchange is free of deadlocks for any number of processes, and rank 0 prints the total.
- Create a C++ console program with MPI in which each rank exchanges an array of
numbers with both neighbors in a ring using the nonblocking operationsMPI_IsendandMPI_IrecvwithMPI_Waitalland prints the sum of the received values. - Create a C++ console program with MPI that distributes an array of
numbers ( is entered by the user and may not be divisible by the number of processes) with theMPI_Scattervoperation, finds the minimum, maximum, and mean, and prints them on rank 0. - Create a C++ console program with MPI that multiplies an
matrix by a vector: the rows of the matrix are distributed withMPI_Scatter, the vector withMPI_Bcast, and the result is collected withMPI_Gatherand checked against sequential multiplication. - Create a C++ console program with MPI following the master–worker scheme, in which rank 0 hands out 1000 numbers one at a time to the workers for primality testing, collects the answers, and prints the number of primes and the number of numbers tested by each worker.
- Create a C++ console program with MPI that creates a two-dimensional periodic Cartesian topology of processes, finds the neighbors of each process with
MPI_Cart_shift, and exchanges ranks with them, and each process prints its coordinates and the ranks of its neighbors. - Create a C++ console program with MPI and OpenMP that initializes MPI with the
MPI_Init_threadfunction withMPI_THREAD_FUNNELEDand computes by integration: the steps are divided among the ranks and, within a rank, among threads withreduction; it prints the number of ranks and threads and the result. - Create a C++ console program with MPI that measures with
MPI_Wtimethe time to compute the sum of numbers for the current number of processes (the time of the slowest rank, the median of 5 runs betweenMPI_Barriercalls) and prints the time, the speedup relative to a sequential computation on rank 0, and the efficiency.
Topic 13. Clusters and the Slurm scheduler
- Create an
sbatchscript for the sequential program./modelwith the argumentsinput.dat 1000: 1 task, 2 GB of memory, a 30 min limit, thedebugpartition, and an output file named with the job name and number; after the program runs, the script prints its exit code and the running time in seconds (theSECONDSvariable). - Create an
sbatchscript for a multithreaded OpenMP program on a single node with 8 CPUs and 4 GB of memory, in which the number of threads is taken from a Slurm variable, thread binding is set withOMP_PLACES=coresandOMP_PROC_BIND=close, and the node, the number of allocated CPUs, and the values of the OpenMP variables are printed before the launch. - Create a C++ MPI program in which each rank prints its number, the number of ranks, and the node name, and rank 0 gathers the node names (
MPI_Gather) and prints how many ranks run on each node; and ansbatchscript for 3 nodes with 4 ranks each, launched throughsrun --mpi=pmix. - Create an
sbatchscript for a hybrid MPI + OpenMP program on 2 nodes with 2 ranks per node and 4 threads per rank: the layout directives, theOMP_NUM_THREADSvariable, and a launch throughsrun; before the launch, the script checks that the number of ranks times the number of threads equals the number of allocated CPUs and exits with code 1 if not. - Create an
sbatchscript with an array of 50 elements (at most 10 at a time) in which element processes the -th line of theparams.txtfile (the parameters of the./simprogram) and writes the result toresults/<i>.txt, and a script that, after the array finishes (a dependency), merges the results and prints the numbers of the elements without a result. - Create a bash script that submits a chain of three jobs
a.sbatch→b.sbatch→c.sbatchwithafterokconditions and acleanup.sbatchjob with anafteranycondition on the last job of the chain, prints the job numbers, and after completion prints a table of the states and exit codes of all four jobs fromsacct. - Create a bash script that prints: the cluster partitions with the number of free and busy nodes (
sinfo), your jobs with their pending reasons (squeuewith a custom format), the expected start time of the pending jobs, and offers to cancel (scancel) jobs that have been pending for more than a number of hours specified by the user. - Create a bash script that, from the output of
sacct -X --parsable2for a period entered by the user, prints a table of jobs “number – name – state – exit code – duration – CPU·h” and a summary: the number of jobs by state and the total CPU·h. - Write a
slurm.conffragment for a cluster with a head nodeheadand nodesnode[01-04](8 CPUs: 1 socket, 4 cores, 2 threads, 16 GB of memory with a reserve for the OS) with core and memory allocation, task binding through cgroup, and the partitionsshort(all nodes, 1 h, default) andlong(nodes 3–4, 3 days); and a bash script that checks the syntax of the fragment (the mandatory parameters, the time format, the existence of the partitions’ nodes). - Create a diagnostic bash script for a compute node that checks and prints as a “parameter – value – recommended” table: the frequency governor, the THP mode,
vm.swappiness,kernel.numa_balancing, thememlockandnofilelimits, the MTU of the network interface, the state of themunge,slurmd, andchronyservices, and the state of the node in Slurm (sinfo -n).
Topic 14. Sockets, RPC, and gRPC
- Create an asynchronous TCP server that serves several clients simultaneously and responds to each line with that line in uppercase, and a client that sends lines from the keyboard; the server prints client connections and disconnections.
- Create a TCP calculator client and server with “length (4 bytes) + JSON” framing: the client sends an operation and two numbers, and the server returns the result or an error message (division by zero, unknown operation).
- Create a UDP server that returns the current time in response to a “TIME” request, and a client that sends 10 requests with a 1 s timeout, retries lost ones, and prints the latency of each request and the number of losses.
- Create a TCP client that connects to a server with retries (3 attempts, exponential backoff, a 2 s connection timeout) and prints the reason for each failure (
SocketError) and a final message. - Describe in a
.protofile a student service with the methodsGetStudent(id)andAddStudent(student), and implement a gRPC server on ASP.NET Core and a client; a nonexistent student returns theNotFoundcode, which the client prints. - Create a gRPC service with server streaming that sends N primes with a 200 ms pause, and a client that prints them; the client sets a 1 s deadline and prints the numbers received and the
DeadlineExceededstatus code. - Create a gRPC service with client streaming that accepts numbers and returns their count, sum, and mean, and a client that sends numbers from a text file.
- Create a gRPC echo service with bidirectional streaming that responds to each message with two messages, and a client that sends and reads messages simultaneously, printing the order in which they are received.
- Create a gRPC service with an interceptor that writes the method name, the duration, and the status code of each call to the console, and a client that passes
client-namemetadata and performs a successful call and a failing one. - Create a CoreWCF service with a
[ServiceContract]contract (the “add” and “list” note operations) and aBasicHttpBindingendpoint, and a console client onSystem.ServiceModel.Httpthat calls both operations and handlesFaultException.
Topic 15. The RabbitMQ broker
- Create a publisher that publishes N messages (N is entered from the keyboard) to a durable RabbitMQ queue, and a consumer with manual acknowledgments that prints each message and their total number.
- Create a work queue with two workers: a job contains its processing duration in milliseconds, and the workers have a prefetch of 1 and manual acknowledgments; print which jobs each worker completed and the total time.
- Create a publisher that publishes log messages to a direct exchange with the keys
info,warning, anderror, and two consumers: the first receives onlyerror, and the second all levels; each prints the messages received. - Create a publisher of events to a fanout exchange and three consumers with durable queues; show that a consumer started later receives the events published before it started.
- Create a publisher of events with
<city>.<type>keys to a topic exchange and a consumer that takes binding patterns as command-line arguments and prints the events received. - Create a publisher with publisher confirms and
mandatory: truethat publishes a message to a queue whose name is entered from the keyboard and prints “confirmed” or the reason for the rejection (PublishException, a return). - Create a
tasksqueue with a dead letter exchange: a consumer rejects messages with odd numbers (requeue: false), and a second consumer of the queue of rejected messages prints them together with the reason from thex-deathheader. - Create an RPC over queues: the server computes the factorial of a number, and the client sends three requests simultaneously with different
CorrelationIdvalues, waits for responses at most 2 s, and prints the results or a timeout message. - Create an idempotent payment consumer: the publisher publishes payments with a
MessageId, some of which are duplicates; the consumer credits each payment once and prints the balance and the number of skipped duplicates. - Create a coordinator that divides the computation of the sum of the squares of the numbers from 1 to N into K parts and publishes them to a queue, and a worker that computes the parts and returns the result; the coordinator prints the sum, checks it with the formula, and prints the time.
Topic 16. Actors and Microsoft Orleans
- Create an Orleans application (a silo and a client in one process) with a counter grain (the key is a string) with
IncrementandGetmethods; the client makes 1000 simultaneousIncrementcalls for two counters and prints the values and the time. - Create a silo and a separate Orleans console client with a user notes grain (the key is a name) with “add” and “list” methods; the client executes commands from the keyboard.
- Create an Orleans application with a user profile grain whose state is stored through
IPersistentStatein a Redis or in-memory provider; after the program restarts, the profile is read again and printed. - Create an Orleans application with a quiz timer grain that, after starting, prints the remaining time every second with a timer (
RegisterGrainTimer) and ends the round after 10 s. - Create an Orleans application with a subscription grain that, with a reminder (an in-memory provider, the minimum period reduced to 5 s), prints a payment message three times and cancels the reminder.
- Create an Orleans application with two grains that call each other in a cycle, and demonstrate a
TimeoutExceptionand its elimination with[Reentrant]orRequestContext.AllowCallChainReentrancy; print the execution time. - Create an Orleans application with a
[StatelessWorker]email validation grain and a statistics grain; the client validates 1000 addresses from a file, and the program prints the number of valid and invalid addresses. - Create a console program that calls an unreliable operation (an exception with a probability of 50%) with retries: 4 attempts, exponential backoff of 100, 200, 400 ms with jitter; print each attempt and a summary.
- Create a console program that models N = 5 replicas and, for entered W and R, performs 10,000 write and read operations, printing the share of stale reads and a check of the condition R + W > N.
- Create a console program with an actor on
Channel<T>that keeps track of goods (receipts, sales, stock); 8 tasks send messages simultaneously, and the program prints the correct stock.
Topic 17. Docker, Kubernetes, Aspire
- Create a multistage
Dockerfilefor an ASP.NET Core web service on .NET 10 with the final imageaspnet:10.0, running as theappuser, and a.dockerignorefile; build the image, run a container publishing port 8080, and check the service. - Create a
compose.yamlwith an API service, a RabbitMQ broker with ahealthcheck, and a worker; the API and the worker start after the broker is ready, the connection string is passed through an environment variable, and the workers are scaled with thedocker compose up --scalecommand. - Create a Docker Compose setup with a web service and Redis in which the Redis data is stored in a named volume; show that the data survives
docker compose downand a repeatedup. - Create Kubernetes manifests for a web service: a Deployment with 3 replicas, a Service of type NodePort, and a ConfigMap with parameters passed through
envFrom; deploy them with thekubectl applycommand and show how the requests are distributed among the pods. - Create a Deployment of an ASP.NET Core web service with
readinessProbe,livenessProbe,requests, andlimits; demonstrate the exclusion of an unready pod from the service and the restart of a container after a failed liveness probe. - Deploy a Deployment of an ASP.NET Core web service and perform a rolling update to a new image version with the parameters
maxSurge: 1andmaxUnavailable: 0, view the revision history, and roll back with thekubectl rollout undocommand. - Create an indexed Kubernetes Job (
completions: 6,parallelism: 3) in which each pod computes the sum of the squares of its range of numbers according toJOB_COMPLETION_INDEX; collect the results from the logs and print the total sum. - Create a HorizontalPodAutoscaler for a compute-intensive web service (from 1 to 5 replicas, a target of 50% CPU) and show the change in the number of replicas under load with the
kubectl get hpa -wcommand. - Create an Aspire solution with an AppHost, ServiceDefaults, a web service, and Redis: the connection string is passed through
WithReference, the service starts after Redis is ready (WaitFor), and the worker has two replicas (WithReplicas); show the resources in the dashboard. - Create a web service in an Aspire solution with a custom OpenTelemetry metric (a request counter with a result tag) and a custom
ActivitySourcespan for a computation; show the metric and the trace in the Aspire dashboard.
Topic 18. Microservice architecture
- Create an Aspire solution with two web services (a product catalog and orders), each with its own PostgreSQL database, and a YARP gateway that routes
/api/catalog/**and/api/orders/**through service discovery; the orders service checks a product by calling the catalog, and a nonexistent product returns code 400. - Create a service that publishes an “order created” event to RabbitMQ and a consumer that processes it idempotently: the IDs of processed messages are stored in an Inbox table, and a redelivery of the same event does not change the result; show this by publishing the event again.
- Create an orchestrated order checkout saga with the steps “reserve product → payment → confirmation”: when the payment is declined, a compensation (releasing the reservation) is performed, the saga state is stored in a database, and a state query returns the step log as a table.
- Implement a Transactional Outbox: the service writes an order and a message to an Outbox table in a single EF Core transaction, and a background relay publishes the messages to RabbitMQ; show that messages are not lost when the broker is stopped.
- Create two web services in which the HTTP client of the first calls the second with a
Microsoft.Extensions.Http.Resilienceresilience policy (a timeout, three retries, a circuit breaker); demonstrate the circuit breaker by stopping the called service, and log its state changes. - Create an Aspire solution with a YARP gateway, two HTTP services, and a RabbitMQ consumer, configure OpenTelemetry tracing for a request that passes through them, add a custom
ActivitySourcespan with attributes, and show a single trace with all spans in the Aspire dashboard. - Create a web service with two API versions (
/v1/...and/v2/...), where the second version extends the response format and the first is marked as deprecated with a response header; show that the old and new clients work simultaneously. - Create an Aspire solution with web services that use PostgreSQL and RabbitMQ and have
/healthand/alivehealth checks, where readiness depends on the availability of the database and the broker; show the change in the resource state in the dashboard after the database is stopped. - Create a YARP gateway in front of a web service with request rate limiting (5 requests per 10 s per client), status code 429, and a
Retry-Afterheader; a console client sends 20 requests and prints a “number – code – time” table. - Create an
Aspire.Hosting.Testingintegration test that starts an AppHost with two services and a gateway, waits for the resources to be ready, makes requests through the gateway, and checks the response codes and content for the success and error scenarios.