Advertisement
sandrovieira

Processamento Paralelo - Fonte

Nov 23rd, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using System.Management;
  9.  
  10. namespace ponto_flutuante
  11. {
  12.     class Program
  13.     {
  14.         static string Tipo_Arquitetura(string x)
  15.         {
  16.             if (Convert.ToInt16(x) == 0)
  17.                 x = ("x86");
  18.             if (Convert.ToInt16(x) == 1)
  19.                 x = ("MIPS");
  20.             if (Convert.ToInt16(x) == 2)
  21.                 x = ("Alpha");
  22.             if (Convert.ToInt16(x) == 3)
  23.                 x = ("PowerPC");
  24.             if (Convert.ToInt16(x) == 5)
  25.                 x = ("ARM");
  26.             if (Convert.ToInt16(x) == 6)
  27.                 x = ("Itanium-based systems");
  28.             if (Convert.ToInt16(x) == 9)
  29.                 x = ("x64");
  30.             return x;
  31.         }
  32.         static string Busca_Sistema(string x)
  33.         {
  34.             double memoriaTotal = 0;
  35.             int contador = 0;
  36.             string str2 = "";
  37.             string niveisDeCache = "";
  38.             string tipoDeCache = "";
  39.             ManagementObjectSearcher searcher = new ManagementObjectSearcher(x);
  40.             foreach (ManagementObject share in searcher.Get())
  41.             {
  42.                 if (x == "select * from Win32_CacheMemory")
  43.                     contador++;
  44.                 foreach (PropertyData PC in share.Properties)
  45.                 {
  46.                     str2 = PC.Name;
  47.                     if (PC.Value != null)
  48.                     {
  49.                         switch (PC.Value.GetType().ToString())
  50.                         {
  51.                             case "System.String":
  52.                                 str2 = (string)PC.Value;
  53.  
  54.                                 break;
  55.                             case "System.UInt16":
  56.                                 ushort shortData = (ushort)PC.Value;
  57.  
  58.                                 str2 = shortData.ToString();
  59.                                 break;
  60.                             case "System.UInt32":
  61.                                 UInt32 longData = (UInt32)PC.Value;
  62.                                 str2 = longData.ToString();
  63.                                 break;
  64.                             case "System.UInt64":
  65.                                 UInt64 verylongData = (UInt64)PC.Value;
  66.                                 str2 = verylongData.ToString();
  67.                                 break;
  68.                         }
  69.                     }
  70.                 } // Fim do segundo foreach
  71.                 if (x == "select Associativity from Win32_CacheMemory")
  72.                     tipoDeCache = tipoDeCache + str2;
  73.                 if (x == ("select Level from Win32_CacheMemory"))
  74.                     niveisDeCache = niveisDeCache + str2;
  75.                 if (x == "select Capacity from Win32_PhysicalMemory")
  76.                     memoriaTotal += Convert.ToDouble(str2) / 1000000;
  77.             } // Fim primeiro foreach
  78.             if (x == "select * from Win32_CacheMemory")
  79.                 return Convert.ToString(contador);
  80.             else
  81.                 if (x == "select Level from Win32_CacheMemory")
  82.                     return niveisDeCache;
  83.                 else
  84.                     if (x == "select Associativity from Win32_CacheMemory")
  85.                         return tipoDeCache;
  86.                     else
  87.                         if (x == "select Capacity from Win32_PhysicalMemory")
  88.                             return Convert.ToString(memoriaTotal);
  89.                         else
  90.                             return str2;
  91.         }      
  92.         static void Main(string[] args)
  93.         {
  94.             Console.Title = "Testes com Processamento Paralelo";
  95.             Program programa = new Program();
  96.             programa.Menu();
  97.         }
  98.         static void calculo_Processamento_Paralelo(bool parametro)      
  99.         {
  100.             Console.Clear();
  101.             Int64 sequencial;                    // Armazena o valor do tempo gasto no processamento sequencial
  102.             Int64 paralelo;                      // Armazena o valor do tempo gasto no processamento paralelo
  103.             int linhas = 1000;
  104.             int colunas = 1000;
  105.             Stopwatch tempo_execucao = new Stopwatch();
  106.             double[,] matriz1 = new double[linhas, colunas];
  107.             double[,] matriz2 = new double[linhas, colunas];
  108.             double[,] matrizResultado = new double[linhas, colunas];
  109.             Random aleatorio = new Random();
  110.             // Geração dos valores das Matrizes
  111.             for (int i = 0; i < linhas; i++)
  112.             {
  113.                 for (int j = 0; j < colunas; j++)
  114.                 {
  115.                     matriz1[i, j] = aleatorio.Next(100);
  116.                     matriz2[i, j] = aleatorio.Next(100);
  117.                 }
  118.             }
  119.             // Multiplicação dos números das Matrizes de forma sequencial (Loop Não-paralelo)
  120.             tempo_execucao.Start();
  121.             for (int i = 0; i < linhas; i++)
  122.             {
  123.                 for (int j = 0; j < colunas; j++)
  124.                 {
  125.                     matrizResultado[i, j] = matriz1[i, j] * matriz2[i, j];
  126.                     if (parametro == true)
  127.                         Console.Write("\t" + matrizResultado[i,j]);
  128.                 }
  129.             }
  130.             tempo_execucao.Stop();
  131.             TimeSpan contador = tempo_execucao.Elapsed;
  132.             sequencial = contador.Milliseconds;  
  133.             tempo_execucao.Reset();
  134.             // Multiplicação dos números das Matrizes de forma paralela (Loop paralelo)
  135.             tempo_execucao.Start();
  136.             Parallel.For(0, linhas, i =>
  137.             {
  138.                 for (int j = 0; j < colunas; j++)
  139.                 {
  140.                     matrizResultado[i, j] = matriz1[i, j] * matriz2[i, j];
  141.                     if (parametro == true)
  142.                         Console.Write("\t" + matrizResultado[i, j]);
  143.                 }                
  144.             });
  145.             tempo_execucao.Stop();
  146.             contador = tempo_execucao.Elapsed;
  147.             paralelo = contador.Milliseconds;
  148.             Console.Clear();
  149.             Console.WriteLine("Tempo gasto para cálculo com processamento sequencial:\t{0:f4}ms\n\nTempo gasto para cálculo com processamento paralelo:\t{1:f4}ms", sequencial, paralelo);
  150.         }
  151.         public void Menu()
  152.         {
  153.             Console.Clear();
  154.             String variavel;
  155.             string x;
  156.             string y;
  157.             Console.WriteLine("\n\t\t\t    - Informações sobre a CPU -\n\n");
  158.             x = ("select Name from Win32_Processor");
  159.             Console.WriteLine("\t\t{0}\n", Busca_Sistema(x));
  160.             x = ("select MaxClockSpeed from Win32_Processor");
  161.             Console.WriteLine("\t\t\tVelocidade da CPU:\t{0}Mhz", Busca_Sistema(x));
  162.             x = ("select NumberOfLogicalProcessors from Win32_Processor");
  163.             y = ("select NumberOfCores from Win32_Processor");
  164.             if (Convert.ToInt16(Busca_Sistema(x)) > Convert.ToInt16(Busca_Sistema(y)))
  165.                 y = ("Hypertreading");
  166.             else
  167.                 y = ("Turboboost");
  168.             Console.WriteLine("\t\t\tTencologia da Cpu:\t{0}", y);
  169.             x = ("select Architecture from Win32_Processor");
  170.             x = Tipo_Arquitetura(Busca_Sistema(x));
  171.             Console.WriteLine("\t\t\tArquitetura da Cpu:\t{0}", x);
  172.             x = ("select NumberOfCores from Win32_Processor");
  173.             Console.WriteLine("\t\t\tNúmero de Núcleos:\t{0}", Busca_Sistema(x));
  174.             Console.WriteLine("\n\t\t\t  :: Escolha um opcão de teste ::");
  175.             Console.Write("\n\n\t\t\t1 - Teste com impressão na tela   \n\n\t\t\t2 - Teste sem impressão na tela   \n\n\t\t\t0 - Sair\n\n\n\n\t\t\t\tEscolha: ");
  176.             variavel = Console.ReadLine();
  177.             if (variavel == "0")
  178.             {
  179.                 Console.Beep();
  180.                 return;
  181.             }
  182.             if (variavel == "1")
  183.             {
  184.                 calculo_Processamento_Paralelo(true);
  185.                 Console.ReadKey();
  186.                 Menu();
  187.             }
  188.             if (variavel == "2")
  189.             {
  190.                 calculo_Processamento_Paralelo(false);
  191.                 Console.ReadKey();
  192.                 Menu();
  193.  
  194.             }
  195.             else
  196.             {
  197.                 Console.ForegroundColor = ConsoleColor.Red;
  198.                 Console.Write("\n\n\tComando invalido. Pressione qualquer tecla pra tentar novamente...");
  199.                 Console.ForegroundColor = ConsoleColor.Gray;
  200.                 Console.ReadKey();
  201.                 Console.Clear();
  202.                 Menu();
  203.             }
  204.         }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement