Open main menu

CDOT Wiki β

Changes

GPU621/Intel Inspector

4,101 bytes added, 17:56, 11 August 2021
no edit summary
=Tutorial=
==Intel Inspector GUI==
We will analyze a simple code with memory leak to demonstrate the steps to use Intel Inspector's Analysis
* You want to determine if memory is leaked during a specific interval of application execution, or during a specific user action.
=Invalid Memory Access= Here we are accessing c[1] which is already deleted. Upon analyzing this code we get invalid memory access error along with the line number where the invalid access occurs. There are screenshots of this analysis after this code. <syntaxhighlight lang="cpp" line='line'>#include<iostream>int main(){ char* c; c = new char[100];//requests heap memory which will not be freed for (int i = 0;i < 100;i++) { c[i] = 'a'; } std::cout << c[10] << std::endl;  delete[] c; c[1] = 'a';}</syntaxhighlight>  [[File:Intel inspector2 - Microsoft Visual Studio 8 11 2021 3 57 42 PM.png|1000px|alt text]]  = Walkthrough = == Memory Leak == === Memory Leak ===   This program is written in C++, it will allocate an array of integer pointers then terminated.
<syntaxhighlight lang="cpp" line='line'>
 
int main()
{
int* myInts = new int[5] ;
 
// incorrectly deallocating an array of pointer
delete myInts ;
return 0 ;
}
 
</syntaxhighlight>
 The result shows there is memory allocation leak on line 5 and it is causing memory leak4.
[[File:memoryLeak_notFreed.png|1200px]]
 The Inspector User Guide has provided the following solutions. [[File:UserGuide MemoryLeak.png|1000px]]   === Mismatched Deallocation and Missing Allocation ===   Try to deallocate the memory by using two delete keyword at the same time. <syntaxhighlight lang="cpp" line='line'> int main(){ int* myInts = new int[5] ;   delete myInts ; // deallocates one object delete[] myInts ; // deallocate an array of object  return 0 ; } </syntaxhighlight>   If there is type mismatched deallocation, Inspector will mark down the allocation and mismatched deallocation line.
[[File:memoryLeak_mismatched.png|1200px]]
In this case, the correct way to deallocate an array of pointer is delete[] myInts.
 There is missing allocation occurred on line 7 because we have deallocated one object in the previous line. [[File:memoryLeak_missing.png|1200px]]   The Inspector User Guide has provided the following solutions. [[File:UserGuide Mismatched.png|1000px]]   = References == Invalid Memory Access ===   Try to assign value to a deallocated object. <syntaxhighlight lang="cpp" line='line'> int main(){ int* myInts = new int[5] ;   delete[] myInts ;  myInts[0] = 2 ;   return 0 ; } </syntaxhighlight>   A problem of type invalid memory access is shown. The lines where we are assigning value to the deallocated object and where the object is being allocated and deallocated are being listed out by the Inspector. [[File:InvalidMemoryAccess.png|1200px]]   Here is a diagram which demonstrate the process of invalid memory access. [[File:UserGuide InvalidMemoryAccess Diagram.png|500px]]   The Inspector User Guide has provided the following solutions. [[File:UserGuide InvalidMemoryAccess.png|1000px]]   == Race Condition == === Code with Race Condition ===   This program is written in C, it will calculate value of pi by calculating the area under a curve and it is using OpenMP library. <syntaxhighlight lang="c" line='line'> #include <stdio.h>#include <stdlib.h>#include <omp.h> int main(){ long long int i, n = 10000000; double x, pi; double sum = 0.0; double step = 1.0 / (double)n; #pragma omp parallel for private(i,x) for (i = 0; i < n; i++) { x = (i + 0.5) * step; sum += 4.0 / (1.0 + x * x); }  pi = step * sum;  printf("pi = %17.15f\n", pi);  return 0;} </syntaxhighlight>   There is a race condition happening on line 15. From the timeline at at the bottom right of the screenshot we can see that thread #1 and #2 are competing to write data to sum variable.  [[File:RaceCondition.png|1200px]]   Here is a diagram which demonstrate the race condition of two threads are trying to write data. [[File:UserGuide RaceCondition Diagram WW.png|800px]]   At this time, thread #0 is trying to read data and thread #6 is trying to write data. This shows that Inspector is able to capture the movement of the threads. [[File:RaceCondition Timeline.png|1200px]]   Here is a diagram which demonstrate the race condition of one thread is trying to read data while the other one is trying to write data. [[File:UserGuide RaceCondition Diagram RW.png|800px]]   The Inspector User Guide has provided the following solutions. [[File:UserGuide RaceCondition.png|1000px]]  === Fixed Code ===   This is the solution to above code. <syntaxhighlight lang="c" line='line'> #include <stdio.h>#include <stdlib.h>#include <omp.h> int main(){ long long int i, n = 10000000; double x, pi; double sum = 0.0; double step = 1.0 / (double)n; #pragma omp parallel for private(i,x) for (i = 0; i < n; i++) { x = (i + 0.5) * step;#pragma omp atomic sum += 4.0 / (1.0 + x * x); }  pi = step * sum;  printf("pi = %17.15f\n", pi);  return 0;} </syntaxhighlight>   = Reference = [http://www.example.com Intel Inspector User Guide]
29
edits