SHARE
TWEET

Untitled

a guest Apr 24th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include<string.h>
  4. #include<cstring>
  5. #include<Windows.h>
  6. #include<string>
  7. #include<iostream>
  8. //#include "CPUID.h"
  9.  
  10. using namespace std;
  11. /*
  12. double CPUSpeed()
  13. {
  14. wchar_t Buffer[_MAX_PATH];
  15. DWORD BufSize = _MAX_PATH;
  16. DWORD dwMHz = _MAX_PATH;
  17. HKEY hKey;
  18.  
  19. // open the key where the proc speed is hidden:
  20. long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  21. L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
  22. 0,
  23. KEY_READ,
  24. &hKey);
  25. if (lError != ERROR_SUCCESS)
  26. {// if the key is not found, tell the user why:
  27. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  28. NULL,
  29. lError,
  30. 0,
  31. Buffer,
  32. _MAX_PATH,
  33. 0);
  34. wprintf(Buffer);
  35. return 0;
  36. }
  37.  
  38. // query the key:
  39. RegQueryValueEx(hKey, L"~MHz", NULL, NULL, (LPBYTE)&dwMHz, &BufSize);
  40. return (double)dwMHz;
  41. }
  42. */
  43. double PCFreq = 0.0;
  44. __int64 CounterStart = 0;
  45.  
  46. void StartCounter()
  47. {
  48.     LARGE_INTEGER li;
  49.     if (!QueryPerformanceFrequency(&li))
  50.         cout << "QueryPerformanceFrequency failed!\n";
  51.  
  52.     PCFreq = double(li.QuadPart) / 1000.0;
  53.  
  54.     QueryPerformanceCounter(&li);
  55.     CounterStart = li.QuadPart;
  56. }
  57. double GetCounter()
  58. {
  59.     LARGE_INTEGER li;
  60.     QueryPerformanceCounter(&li);
  61.     return double(li.QuadPart - CounterStart) / PCFreq;
  62. }
  63.  
  64.  
  65. int main()
  66. {
  67.     unsigned long long start = 0, FamilyCode = 0, ExtendedFamily = 0, stop = 0, brand_table = 0, brand_table_count = 0, regA,ModelNr=0,Type=0,ExtendedModel=0;
  68.     string vendor;
  69.     //char vendor[12];
  70.     __asm
  71.     {
  72.         rdtsc; EDX, EAX
  73.             MOV DWORD PTR start + 4, EDX
  74.             OR DWORD PTR start, EAX
  75.     }
  76.     Sleep(1000);
  77.     __asm
  78.     {
  79.         rdtsc; EDX, EAX
  80.             MOV DWORD PTR stop + 4, EDX
  81.             OR DWORD PTR stop, EAX
  82.     }
  83.     __asm
  84.     {
  85.         mov eax, 1
  86.             cpuid
  87.             mov dword ptr regA, EAX
  88.             //  MOV DWORD PTR vendor, EBX
  89.             //MOV DWORD PTR vendor + 4, EDX
  90.             //MOV DWORD PTR vendor + 8, ECX
  91.             MOV DWORD PTR vendor, EAX
  92.  
  93.             /*
  94.            
  95.             //print Brand ID
  96.             CMP     _cpu_type,6
  97.             JB      print_486_type
  98.             JA      print_pentiumiiimodel18_type
  99.             MOV     EAX,DWORD PTR _cpu_signature
  100.             SHR      EAX,4
  101.             AND     AL,0Fh
  102.             CMP     AL, 8
  103.             JNE     print_pentiumiiimodel18_type
  104.  
  105.             print_pentiumii18_type:
  106.             CMP EAX,8
  107.             JB print_unknown_type
  108.             MOV EAX, DWORD PTR _features_ebx
  109.             OR AL,AL
  110.             JZ print_unknown_type
  111.             MOV DI, offset brand_table
  112.             MOV XC, brand_table_count
  113.             */
  114.     }
  115.     /*
  116.  
  117.     //CPUID cpuID(0); // Get CPU vendor
  118.  
  119.  
  120.     //vendor += string((const char *)&cpuID.EBX(), 4);
  121.     //vendor += string((const char *)&cpuID.EDX(), 4);
  122.     //vendor += string((const char *)&cpuID.ECX(), 4);
  123.  
  124.     //cout << "CPU vendor = " << vendor << endl;
  125.  
  126.     //CPUID cpuID2(1);
  127.     //FamilyCode = (regA >> 20) & 0xFF + (regA >> 8) & 0x0F
  128.     */
  129.     ModelNr = (regA >> 4) & 0x0F;
  130.     FamilyCode = (regA >> 8) & 0x0F;
  131.     Type = (regA >>12)& 0x03;
  132.     ExtendedModel = (regA >> 16) & 0x0F;
  133.     ExtendedFamily = (regA >> 20) & 0xFF;
  134.    
  135.     //printf(" ModelNr=%d\n FamilyCode=%d\n Type=%d\n ExtendedModel=%d\n ExtendedFamily=%d", ModelNr, FamilyCode, Type, ExtendedModel, ExtendedFamily);
  136.     printf(" ModelNr=%d\n", ModelNr);
  137.     printf("FamilyCode =%d\n", FamilyCode);
  138.     printf("Type =%d\n", Type);
  139.     printf("ExtendedModel =%d\n", ExtendedModel);
  140.     printf("ExtendedFamily =%d\n", ExtendedFamily);
  141.  
  142.     //F = CPUID(1).EAX[27:20] + CPUID(1).EAX[11:8];
  143.     //printf("\n Vendor : %s \n", vendor);
  144.  
  145.     //rdtsc();
  146.     //wait(1000);
  147.     //rdtsc(stop)
  148.     double freq = (stop - start) / 1000000.0;
  149.     cout << "\nFreq : " << freq << "MHz\n";
  150.     /* ex1
  151.     cout << "\nCerinta 1\n\n";
  152.     cout << "CPU speed : " << CPUSpeed() << "MHz";
  153.     */
  154.     cout << "\nCerinta 2\n\n";
  155.     // ex2
  156.     //current_thread=GetCurrentProcess();
  157.     int core = 2;
  158.     cout << SetThreadAffinityMask(GetCurrentThread(), (1 << core) - 1);
  159.  
  160.     //ex3
  161.     cout << "\n Cerinta 3\n\n";
  162.  
  163.     /*
  164.  
  165.     //high priority
  166.     SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  167.     StartCounter();
  168.     for (int i = 0; i < 10000; i++)
  169.     for (int j = 0; j < 10000; j++)
  170.     for (int k = 0; k < 5; k++)
  171.     {
  172.  
  173.     }
  174.  
  175.     cout << "Timp process high priority : " << GetCounter();
  176.     cout << "\n";
  177.  
  178.     //normal priority
  179.     SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
  180.     StartCounter();
  181.     for (int i = 0; i < 10000; i++)
  182.     for (int j = 0; j < 10000; j++)
  183.     for (int k = 0; k < 5; k++)
  184.     {
  185.  
  186.     }
  187.  
  188.     cout << "Timp process normal priority : " << GetCounter();
  189.     cout << "\n";
  190.  
  191.     //idle priority
  192.     SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  193.     StartCounter();
  194.     for (int i = 0; i < 10000; i++)
  195.     for (int j = 0; j < 10000; j++)
  196.     for (int k = 0; k < 5; k++)
  197.     {
  198.  
  199.     }
  200.  
  201.     cout << "Timp process normal idle : " << GetCounter();
  202.     cout << "\n";
  203.  
  204.  
  205.  
  206.     //ex4
  207.     cout << "\nCerinta 4\n\n";
  208.  
  209.  
  210.     //highest priority
  211.  
  212.     SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
  213.     StartCounter();
  214.     for (int i = 0; i < 10000; i++)
  215.     for (int j = 0; j < 10000; j++)
  216.     for (int k = 0; k < 5; k++)
  217.     {
  218.  
  219.     }
  220.  
  221.     cout << "Timp thread highest : " << GetCounter();
  222.     cout << "\n";
  223.  
  224.  
  225.     //above_normal priority
  226.     SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  227.     StartCounter();
  228.     for (int i = 0; i < 10000; i++)
  229.     for (int j = 0; j < 10000; j++)
  230.     for (int k = 0; k < 5; k++)
  231.     {
  232.  
  233.     }
  234.  
  235.     cout << "Timp thread above normal : " << GetCounter();
  236.     cout << "\n";
  237.  
  238.  
  239.     //below normal priority
  240.     SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
  241.     StartCounter();
  242.     for (int i = 0; i < 10000; i++)
  243.     for (int j = 0; j < 10000; j++)
  244.     for (int k = 0; k < 5; k++)
  245.     {
  246.  
  247.     }
  248.  
  249.     cout << "Timp thread below normal : " << GetCounter();
  250.     cout << "\n";
  251.     //system("PAUSE");
  252.    
  253.     */
  254.  
  255.     // lab 6 ( lab 5 )
  256.  
  257. // add 2 vect 4 elem normal
  258.  
  259.     cout << "add 2 vect 4 elem normal \n\n";
  260.     float v1[] = { 0, 1, 2, 3 }, v2[] = {5,6,7,8}, v3[4];
  261.     int i = 0;
  262.     printf("Vector suma :");
  263.     StartCounter();
  264.     for (i = 0; i < 4; i++)
  265.     {
  266.         v3[i] = v1[i] + v2[i];
  267.         printf("%f ", v3[i]);
  268.     }
  269.     cout<<"\n A durat : "<< GetCounter()<<"\n";
  270.  
  271. // add 2 vect 4 elem SSE
  272.  
  273.     cout << "\n\nadd 2 vect 4 elem SSE \n\n";
  274.     float adr1,adr2;
  275.     __m128 _mm_load_ps(float const* adr1);
  276.     __m128 _mm_load_ps(float const* adr2);
  277.     StartCounter();
  278.     __m128 _mm_add_ps(__m128 adr1, __m128 adr2);
  279.     cout << "\n A durat : " << GetCounter() << "\n";
  280.  
  281. //Inmultirea unui vector cu un scalar folosind instructiuni instructiuni obisnuite
  282.  
  283.     cout << "\nInmultirea unui vector cu un scalar folosind instructiuni instructiuni obisnuite\n\n";
  284.     float v4[] = { 0, 1, 2, 3 }, v6[4];
  285.     printf("Vector inmult :");
  286.     StartCounter();
  287.     for (i = 0; i < 4; i++)
  288.     {
  289.         v6[i] = 2 * v4[i];
  290.         printf("%f ", v6[i]);
  291.     }
  292.     cout << "\n A durat : " << GetCounter() << "\n";
  293.  
  294.  
  295. //Inmultirea unui vector cu un scalar folosind instructiuni SSE;
  296.  
  297.     cout << "\n Inmultirea unui vector cu un scalar folosind instructiuni SSE\n\n";
  298.     __m128 _mm_set1_ps(float f);
  299.     __m128 _mm_load_ps(float const* adr2);
  300.     StartCounter();
  301.     __m128 _mm_mul_ps(__m128 f, __m128 adr2);
  302.     cout << "\n A durat : " << GetCounter() << "\n";
  303.  
  304.  
  305. //Inmultirea unei matrici de dimensiune 4x4 cu un vector de 4 elemente folosind instructiuni obisnuite
  306.  
  307.     cout << "\n Inmultirea unei matrici de dimensiune 4x4 cu un vector de 4 elemente folosind instructiuni obisnuite\n\n";
  308.     float m1[4][4] = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }, { 12, 13, 14, 15 } }, vec[] = { 1, 2, 3, 4 }, r1[4] = {0,0,0,0};
  309.     int j = 0;
  310.     cout << "Rez : ";
  311.     StartCounter();
  312.     for (i = 0; i < 4; i++)
  313.     {
  314.         for (j = 0; j < 4; j++)
  315.         {
  316.             r1[i] = m1[i][j] * vec[i] + r1[i];
  317.         }
  318.         cout <<r1[i]<<", ";StartCounter();
  319.     }
  320.     cout << "\n A durat : " << GetCounter() << "\n";
  321.  
  322.  
  323. // Inmultirea unei matrici de dimensiune 4x4 cu un vector de 4 elemente folosind instructiuni SSE
  324.  
  325.     cout << "\n Inmultirea unei matrici de dimensiune 4x4 cu un vector de 4 elemente folosind instructiuni SSE\n\n";
  326.     __m128 _mm_set1_ps(float f);
  327.     __m128 _mm_load_ps(float const* adr2);
  328.     StartCounter();
  329.     __m128 _mm_mul_ps(__m128 f, __m128 adr2);
  330.     cout << "\n A durat : " << GetCounter() << "\n";
  331.  
  332.  
  333.  
  334.  
  335.  
  336.     return 0;
  337. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top