HPMC User Guide v 1.00
© 2026 Bassem W. Jamaleddine
This chapter shows how to use your HPMC calculator to multiply matrices using openMP. The operations will be offloaded to the Intel Xeon Phi MIC.
Consider the program AxB.c shown in the following listing:
1. #ifndef MIC_DEV 2. #define MIC_DEV 0 3. #endif 4. 5. #include <stdio.h> 6. #include <stdlib.h> 7. #include <omp.h> 8. #include <math.h> 9. #include <sys/time.h> 10. 11. double getTime(void) { 12. struct timeval t; 13. gettimeofday(&t,0); 14. return ((double)t.tv_sec + ((double)t.tv_usec / 1000000.0)); 15. } 16. 17. // ################################ 18. // openMP multiply matrices 19. void doMult(int size, float (* restrict A)[size], 20. float (* restrict B)[size], float (* restrict C)[size] 21. ) { 22. 23. #pragma offload target(mic:MIC_DEV) \ 24. in(A:length(size*size)) in( B:length(size*size)) \ 25. out(C:length(size*size)) 26. { 27. #pragma omp parallel for default(none) shared(C,size) 28. for (int i = 0; i < size; ++i) 29. for (int j = 0; j < size; ++j) 30. C[i][j] =0.f; 31. // matrix multiplication. 32. #pragma omp parallel for default(none) shared(A,B,C,size) 33. for (int i = 0; i < size; ++i) 34. for (int k = 0; k < size; ++k) 35. for (int j = 0; j < size; ++j) 36. C[i][j] += A[i][k] * B[k][j]; 37. } 38. } 39. 40. // ################################ 41. int main(int argc, char *argv[]) { 42. if(argc != 4) { 43. fprintf(stderr,"Use: %s size nThreads iter\n",argv[0]); 44. return -1; 45. } 46. int i,j,k; 47. int size=atoi(argv[1]); 48. int nThreads=atoi(argv[2]); 49. int iter=atoi(argv[3]); 50. omp_set_num_threads(nThreads); 51. float (*restrict A)[size] = malloc(sizeof(float)*size*size); 52. float (*restrict B)[size] = malloc(sizeof(float)*size*size); 53. float (*restrict C)[size] = malloc(sizeof(float)*size*size); 54. // init natrices A and B 55. #pragma omp parallel for default(none) shared(A,B,size) private(i,j,k) 56. for (i = 0; i < size; ++i) { 57. for (j = 0; j < size; ++j) { 58. A[i][j] = (float)((rand() % 3)-1); 59. B[i][j] = (float)((rand() % 3)-1); 60. } 61. } 62. // warmup 63. doMult(size, A,B,C); 64. double startTime = getTime(); 65. for (int i=0; i < iter; i++) { 66. doMult(size, A,B,C); 67. } 68. double endTime = getTime(); 69. double lapsed = endTime-startTime; 70. lapsed /= iter; 71. 72. printf("%s nThrds %d size %d elapsedtime %g GFlop/s %g\n", 73. argv[0], nThreads, size, lapsed, 2e-9*size*size*size/lapsed); 74. 75. free(A); free(B); free(C); 76. return 0; 77. } 78.
Compile AxB.c to be executed on the MIC:
# icc -O3 -mmic -qopenmp -L /opt/intel/lib/mic -Wno-unknown-pragmas -std=c99 -liomp5 AxB.c -o AxB.mic
The following script will run the pogram in a loop, incrementing the number of threads by 24 in each iteration up to a maximum of 340 threads. The thread affinity selected is balanced. Possible values are: compact, scatter, balanced.
#!/bin/ksh export KMP_AFFINITY="granularity=thread,balanced" i=24 while [ $i -lt 340 ] do xphi AxB.mic -a "1000 $i 10" let i+=24; done
Notice the COI daemon (coi_daemon) running and managing offload activity. When COI (coprocessor offload infrastructure) is running, it will use on the Xeon Phi side four service threads to manage offload activity.
03:30 root@HPMC9: /mm03fs/DOBB # ./AxB.mic.sh
03:30 root@HPMC9: /mm03fs/DOBB # ./AxB.mic.sh /tmp/coi_procs/1/23650/AxB.mic nThrds 24 size 1000 elapsedtime 0.0400838 GFlop/s 49.8955 /tmp/coi_procs/1/23685/AxB.mic nThrds 48 size 1000 elapsedtime 0.0269411 GFlop/s 74.236 /tmp/coi_procs/1/23743/AxB.mic nThrds 72 size 1000 elapsedtime 0.027996 GFlop/s 71.4388 /tmp/coi_procs/1/23825/AxB.mic nThrds 96 size 1000 elapsedtime 0.0311322 GFlop/s 64.2422 /tmp/coi_procs/1/23931/AxB.mic nThrds 120 size 1000 elapsedtime 0.0186215 GFlop/s 107.403 /tmp/coi_procs/1/24060/AxB.mic nThrds 144 size 1000 elapsedtime 0.0262353 GFlop/s 76.2332 /tmp/coi_procs/1/24215/AxB.mic nThrds 168 size 1000 elapsedtime 0.0202382 GFlop/s 98.823 /tmp/coi_procs/1/24393/AxB.mic nThrds 192 size 1000 elapsedtime 0.0206483 GFlop/s 96.8603 /tmp/coi_procs/1/24595/AxB.mic nThrds 216 size 1000 elapsedtime 0.0144107 GFlop/s 138.786 /tmp/coi_procs/1/24822/AxB.mic nThrds 240 size 1000 elapsedtime 0.0197546 GFlop/s 101.242 /tmp/coi_procs/1/25071/AxB.mic nThrds 264 size 1000 elapsedtime 0.0258059 GFlop/s 77.5017 /tmp/coi_procs/1/25345/AxB.mic nThrds 288 size 1000 elapsedtime 0.0331669 GFlop/s 60.3011 /tmp/coi_procs/1/25643/AxB.mic nThrds 312 size 1000 elapsedtime 0.0454103 GFlop/s 44.0429 /tmp/coi_procs/1/25965/AxB.mic nThrds 336 size 1000 elapsedtime 0.0446899 GFlop/s 44.7528
On the MIC you can see the processes being executed as shown below.
(uos/HPMC9) 03:21 root@mic0: ~ # pstree -up
(HPMC9) 03:21 root@mic0: ~ # pstree -up
init(1)-+-coi_daemon(4354,micuser)-+-AxB.mic(25071)-+-{AxB.mic}(25077)
| | |-{AxB.mic}(25078)
| | |-{AxB.mic}(25079)
| | |-{AxB.mic}(25080)
| | |-{AxB.mic}(25081)
| | |-{AxB.mic}(25082)
...
| | |-{AxB.mic}(25335)
| | |-{AxB.mic}(25336)
| | |-{AxB.mic}(25337)
| | |-{AxB.mic}(25338)
| | |-{AxB.mic}(25339)
| | `-{AxB.mic}(25340)
| |-{coi_daemon}(4356)
| `-{coi_daemon}(25073)
|-getty(4370)
|-klogd(4333)
|-mpssd(4339)-+-{mpssd}(4341)
| `-{mpssd}(4342)
|-portmap(4290,1)
|-sshd(4322)-+-sshd(4382)---bash(4384)---dint(23639)---cat(25342)
| |-sshd(13638)---bash(13640)---dcpus(23643)
| `-sshd(13642)---bash(13644)---pstree(25341)
|-syslogd(4330)
`-udevd(4113)-+-udevd(4336)
`-udevd(4363)
When running an OpenMP program, the number of threads is not limited to the number of cores of the CPU. Same as when running Java threads on a machine with a single core CPU, as the threads are managed by the compiler and the underlying system. However one can use the following equation to select the number of threads for maximum performance of a program to be offloaded to the MIC.
No. of threads to create >= K * T * (N-1)
K = Positive integer (=2 works fine)
T = No. of thread contexts on hardware (4 in the case of MIC 71xx)
N = No. of cores present on hardware (61 in case of MIC 71xx)
Take away one as one of the cores is busy running the Linux micro-OS services
K = Positive integer (=2 works fine)
T = No. of thread contexts on hardware (4 in the case of MIC 71xx)
N = No. of cores present on hardware (61 in case of MIC 71xx)
Take away one as one of the cores is busy running the Linux micro-OS services
■ Interrupt and Stat Activities of a Program Offloaded to MIC
As the program is executed in a loop, and it is being offloaded to the MIC, we can reveal the stats of the cores. We use dcpus to display the stats by looping 24 times with a delay of 3 seconds in between. Since MIC has 228 cores, we use the -prune 6,3 option to display only the first six cores and the last three cores. Notice how the system and user usage went from 0 to higher numbers on the top most cores. As the loop is running, and more threads are allocated, the system usage is also increased on tne upper cores.
This section is not included in the SAMPLE PREVIEW.
On the MIC we can reveal the interrupt activities as the the offloaded program is being run. As the program is offloaded to the MIC, and it is being executed in a loop, we want to reveal the interrupt activities of the MIC. We use dint to display the interrupts (LOC,TLB,RES,TRM,CAL) by looping 24 times with a delay of 3 seconds in between. Since MIC has 228 cores, we use the -prune 6,3 option to display only the first six cores and the last three cores. Notice how the system interrupts of RES,LOC are being increased on the top most cores. As the loop is repeated, and more threads are allocated, the system interrupts also increased on the upper cores.
(uos/HPMC9) 03:30 root@mic0: /tools # ./dint -transpose -iter 24 -delay 3 -prune 6,3 -transpose -interrupts LOC,TLB,RES,TRM,CAL -sortby RES
This section is not included in the SAMPLE PREVIEW.