daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest Jan 20th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <thread>
  4. #include <iostream>
  5. #include <thread>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <string>
  9.  
  10. typedef int(__stdcall *AsmFunction)(int* tab, int size);
  11. typedef void(__stdcall *CppFunction)(int*, int);
  12.  
  13.  
  14. void loadData(unsigned char info[54], unsigned char* & data, int& numberOfElements, char* fileName) {
  15.     FILE* file = fopen(fileName, "rb");
  16.  
  17.     if (file) {
  18.         fread(info, sizeof(unsigned char), 54, file); // read the header
  19.  
  20.                                                       //Extracting image height and width from the header
  21.         int width = *(int*)&info[18];
  22.         int height = *(int*)&info[22];
  23.         width = width + (width % 4);
  24.  
  25.         //Calculating number of bytes required to store all BGR values
  26.         numberOfElements = 3 * width * height;
  27.  
  28.         data = new unsigned char[numberOfElements];
  29.         unsigned char* datatmp = new unsigned char[numberOfElements];
  30.         fread(datatmp, sizeof(unsigned char), numberOfElements, file); // Reading the rest of the data at once
  31.  
  32.         for (int i = 0; i < numberOfElements; i++) {
  33.             if (i % 3 == 0)
  34.                 data[(i / 3)] = datatmp[i];
  35.             if (i % 3 == 1)
  36.                 data[(numberOfElements / 3) + (i / 3)] = datatmp[i];
  37.             if (i % 3 == 2)
  38.                 data[2 * (numberOfElements / 3) + (i / 3)] = datatmp[i];
  39.         }
  40.  
  41.     }
  42. }
  43.  
  44. void func(char x)
  45. {
  46.     std::cout << x;
  47. }
  48. void saveData(unsigned char info[54], unsigned char* & data, int& numberOfElements)
  49.  
  50. {
  51.     unsigned char* datatmp = new unsigned char[numberOfElements];
  52.  
  53.     for (int i = 0; i < numberOfElements / 3; i++) {
  54.         datatmp[3 * i] = data[i];
  55.         datatmp[3 * i + 1] = data[i + (numberOfElements / 3)];
  56.         datatmp[3 * i + 2] = data[i + 2 * (numberOfElements / 3)];
  57.     }
  58.  
  59.     data = datatmp;
  60.  
  61.     FILE* saveFile = fopen("Result.bmp", "wb");
  62.  
  63.     fwrite(info, sizeof(unsigned char), 54, saveFile);
  64.     fwrite(data, sizeof(unsigned char), numberOfElements, saveFile);
  65.  
  66.     fclose(saveFile);
  67. }
  68. //argv[1] - file name, argv[2] - number of threads
  69. int main(int argc, char** argv) {
  70.  
  71.     if (argc != 3)
  72.         return 0;
  73.  
  74.     //Loading functions
  75.     HINSTANCE handleToAsmDll = LoadLibraryA("JADll");
  76.     HINSTANCE handleToCppDll = LoadLibraryA("CPPDll");
  77.     AsmFunction myAsmFunction;
  78.     CppFunction myCppFunction;
  79.  
  80.     if (handleToAsmDll != NULL || handleToCppDll != NULL)
  81.     {
  82.         myAsmFunction = (AsmFunction)GetProcAddress(handleToAsmDll, "MyProc1");
  83.         myCppFunction = (CppFunction)GetProcAddress(handleToCppDll, "decolorization");
  84.     }
  85.     else
  86.     {
  87.         return 0;
  88.     }
  89.  
  90.     if (myAsmFunction == NULL || myCppFunction == NULL)
  91.     {
  92.         return 0;
  93.     }
  94.  
  95.  
  96.     //Checking number concurent threads supported (not number of cores)
  97.     const int concurentThreadsSupported = std::thread::hardware_concurrency();
  98.  
  99.     int numberOfThreads = std::stoi(argv[2]);
  100.  
  101.     unsigned char* data;
  102.     int numberOfElements;
  103.     unsigned char info[54];
  104.  
  105.     //Loading data
  106.     loadData(info, data, numberOfElements, argv[1]);
  107.  
  108.     int* data_int = new int[numberOfElements];
  109.  
  110.     for (int i = 0; i < numberOfElements; i++)
  111.         data_int[i] = (int)data[i];
  112.  
  113.  
  114.     //Creating vector of threads
  115.     std::vector <std::thread> vectorOfThreads;
  116.  
  117.     //Calculating number of elements for each thread
  118.     int elementsPerThread = ceil((double)numberOfElements / numberOfThreads);
  119.  
  120.     if (elementsPerThread % 3 == 2)
  121.         elementsPerThread += 2;
  122.  
  123.     if (elementsPerThread % 3 == 1)
  124.         elementsPerThread += 1;
  125.  
  126.     //Splitting data into smaller portions
  127.     int** portionedData = new int*[numberOfThreads];
  128.  
  129.     for (int i = 0; i < numberOfThreads - 1; ++i)
  130.         portionedData[i] = new int[elementsPerThread];
  131.     portionedData[numberOfThreads - 1] = new int[numberOfElements - (elementsPerThread * (numberOfThreads - 1))];
  132.  
  133.     for (int j = 0; j < numberOfThreads - 1; j++) {
  134.         for (int i = 0; i < elementsPerThread; i++) {
  135.         /*  if (i + j * elementsPerThread <= numberOfElements / 3)
  136.                 break;*/
  137.             if (i % 3 == 0)
  138.                 portionedData[j][i / 3] = data_int[i/3 + (j * elementsPerThread) / 3];
  139.  
  140.             if (i % 3 == 1)
  141.                 portionedData[j][i / 3 + (elementsPerThread / 3)] = data_int[i/3 + (numberOfElements / 3) + (j * elementsPerThread) / 3];
  142.  
  143.             if (i % 3 == 2)
  144.                 portionedData[j][i / 3 + 2 * (elementsPerThread / 3)] = data_int[i/3 + 2 * (numberOfElements / 3) + (j * elementsPerThread) / 3];
  145.            
  146.         }
  147.     }
  148.  
  149.     for (int i = 0; i < numberOfElements - (elementsPerThread * (numberOfThreads - 1)); i++) {
  150.         if (i <= numberOfElements)
  151.             break;
  152.         if (i % 3 == 0)
  153.             portionedData[numberOfThreads - 1][i / 3] = data_int[i / 3 + ((numberOfThreads - 1) * elementsPerThread) / 3];
  154.  
  155.         if (i % 3 == 1)
  156.             portionedData[numberOfThreads - 1][i / 3 + (elementsPerThread / 3)] = data_int[i / 3 + (numberOfElements / 3) + ((numberOfThreads - 1) * elementsPerThread) / 3];
  157.  
  158.         if (i % 3 == 2)
  159.             portionedData[numberOfThreads - 1][i / 3 + 2 * (elementsPerThread / 3)] = data_int[i / 3 + 2 * (numberOfElements / 3) + ((numberOfThreads - 1) * elementsPerThread) / 3];
  160.     }
  161.  
  162.  
  163.     std::chrono::steady_clock::duration time;
  164.     auto start = std::chrono::steady_clock::now();
  165.  
  166.     ////Starting all threads but the last one
  167.     //for (int i = 0; i < numberOfThreads - 1; i++)
  168.     //{
  169.     //  std::thread thread(myCppFunction, portionedData[i + 1], elementsPerThread);
  170.     //  vectorOfThreads.push_back(std::move(thread)); //Thread can't be copied - has to be moved
  171.     //}
  172.     ////Starting last thread
  173.     //std::thread thread(myCppFunction, portionedData[numberOfThreads - 1], numberOfElements - (elementsPerThread * (numberOfThreads - 1)));
  174.     //vectorOfThreads.push_back(std::move(thread));
  175.  
  176.     ////Joining all threads
  177.     //for (int i = 0; i < numberOfThreads - 1; i++)
  178.     //{
  179.     //  vectorOfThreads.at(i).join();
  180.     //}
  181.  
  182.     auto end = std::chrono::steady_clock::now();
  183.     time = end - start;
  184.     std::cout << std::chrono::duration_cast<std::chrono::microseconds>(time).count();
  185.  
  186.     for (int i = 0; i < numberOfThreads - 1; i++)
  187.         myCppFunction(portionedData[i], elementsPerThread);
  188.  
  189.     myCppFunction(portionedData[numberOfThreads - 1], numberOfElements - (elementsPerThread * (numberOfThreads - 1)));
  190.  
  191.     for (int j = 0; j < numberOfThreads - 1; j++) {
  192.         for (int i = 0; i < elementsPerThread; i++) {
  193.         /*  if (i + j * elementsPerThread <= numberOfElements / 3)
  194.                 break;*/
  195.             if (i % 3 == 0)
  196.                 data_int[i / 3 + (j * elementsPerThread) / 3] = portionedData[j][i / 3];
  197.  
  198.             if (i % 3 == 1)
  199.                 data_int[i / 3 + (numberOfElements / 3) + (j * elementsPerThread) / 3] = portionedData[j][i / 3 + (elementsPerThread / 3)];
  200.  
  201.             if (i % 3 == 2)
  202.                 data_int[i / 3 + 2 * (numberOfElements / 3) + (j * elementsPerThread) / 3] = portionedData[j][i / 3 + 2 * (elementsPerThread / 3)];
  203.         }
  204.     }
  205.  
  206.     for (int i = 0; i < numberOfElements - (elementsPerThread * (numberOfThreads - 1)); i++) {
  207.         if (i % 3 == 0)
  208.             data_int[i / 3 + ((numberOfThreads - 1) * elementsPerThread) / 3] = portionedData[numberOfThreads - 1][i / 3];
  209.  
  210.         if (i % 3 == 1)
  211.             data_int[i / 3 + (numberOfElements / 3) + ((numberOfThreads - 1) * elementsPerThread) / 3] = portionedData[numberOfThreads - 1][i / 3 + (elementsPerThread / 3)];
  212.  
  213.         if (i % 3 == 2)
  214.             data_int[i / 3 + 2 * (numberOfElements / 3) + ((numberOfThreads - 1) * elementsPerThread) / 3] = portionedData[numberOfThreads - 1][i / 3 + 2 * (elementsPerThread / 3)];
  215.     }
  216.  
  217.  
  218.     //myAsmFunction(data_int, numberOfElements);
  219.     //myCppFunction(data_int, numberOfElements);
  220.  
  221.     for (int i = 0; i < numberOfElements; i++)
  222.         data[i] = (unsigned char)data_int[i];
  223.  
  224.     saveData(info, data, numberOfElements);
  225.  
  226.     return 0;
  227. }
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
 
Top