Download Intel(R) Math Kernel Library for Linux* OS User's Guide

Transcript
6
Intel® Math Kernel Library User’s Guide
•
OpenMP facilities (recommended, if available), for example, the KMP_AFFINITY
environment variable using the Intel OpenMP library
•
A system function, as explained below
Consider the following performance issue:
•
The system has two sockets with two cores each, for a total of four cores (CPUs).
•
The two-thread parallel application that calls the Intel MKL FFT happens to run faster
than in four threads, but the performance in two threads is very unstable
Example 6-3 resolves this issue. The code example calls the system function
sched_setaffinity to bind the threads to the cores on different sockets. Then the Intel
MKL FFT function is called.
Compile your application with the Intel compiler using the following command:
icc test_application.c –openmp
where test_application.c is the filename for the application.
Build the application. Run it in two threads, for example, by using the environment variable
to set the number of threads:
env OMP_NUM_THREADS=2 ./a.out
Example 6-3 Setting An affinity Mask by Operating System Means Using the Intel® Compiler
#define _GNU_SOURCE //for using the GNU CPU affinity
// (works with the appropriate kernel and glibc)
// Set affinity mask
#include <sched.h>
#include <stdio.h>
#include <unistd.h>
#include <omp.h>
int main(void) {
int NCPUs = sysconf(_SC_NPROCESSORS_CONF);
printf("Using thread affinity on %i NCPUs\n", NCPUs);
#pragma omp parallel default(shared)
{
cpu_set_t new_mask;
cpu_set_t was_mask;
int tid = omp_get_thread_num();
CPU_ZERO(&new_mask);
// 2 packages x 2 cores/pkg x 1 threads/core (4 total cores)
CPU_SET(tid==0 ? 0 : 2, &new_mask);
6-16