Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Diagnostics;
- using System.Threading;
- using System.Management;
- namespace ponto_flutuante
- {
- class Program
- {
- static string Tipo_Arquitetura(string x)
- {
- if (Convert.ToInt16(x) == 0)
- x = ("x86");
- if (Convert.ToInt16(x) == 1)
- x = ("MIPS");
- if (Convert.ToInt16(x) == 2)
- x = ("Alpha");
- if (Convert.ToInt16(x) == 3)
- x = ("PowerPC");
- if (Convert.ToInt16(x) == 5)
- x = ("ARM");
- if (Convert.ToInt16(x) == 6)
- x = ("Itanium-based systems");
- if (Convert.ToInt16(x) == 9)
- x = ("x64");
- return x;
- }
- static string Busca_Sistema(string x)
- {
- double memoriaTotal = 0;
- int contador = 0;
- string str2 = "";
- string niveisDeCache = "";
- string tipoDeCache = "";
- ManagementObjectSearcher searcher = new ManagementObjectSearcher(x);
- foreach (ManagementObject share in searcher.Get())
- {
- if (x == "select * from Win32_CacheMemory")
- contador++;
- foreach (PropertyData PC in share.Properties)
- {
- str2 = PC.Name;
- if (PC.Value != null)
- {
- switch (PC.Value.GetType().ToString())
- {
- case "System.String":
- str2 = (string)PC.Value;
- break;
- case "System.UInt16":
- ushort shortData = (ushort)PC.Value;
- str2 = shortData.ToString();
- break;
- case "System.UInt32":
- UInt32 longData = (UInt32)PC.Value;
- str2 = longData.ToString();
- break;
- case "System.UInt64":
- UInt64 verylongData = (UInt64)PC.Value;
- str2 = verylongData.ToString();
- break;
- }
- }
- } // Fim do segundo foreach
- if (x == "select Associativity from Win32_CacheMemory")
- tipoDeCache = tipoDeCache + str2;
- if (x == ("select Level from Win32_CacheMemory"))
- niveisDeCache = niveisDeCache + str2;
- if (x == "select Capacity from Win32_PhysicalMemory")
- memoriaTotal += Convert.ToDouble(str2) / 1000000;
- } // Fim primeiro foreach
- if (x == "select * from Win32_CacheMemory")
- return Convert.ToString(contador);
- else
- if (x == "select Level from Win32_CacheMemory")
- return niveisDeCache;
- else
- if (x == "select Associativity from Win32_CacheMemory")
- return tipoDeCache;
- else
- if (x == "select Capacity from Win32_PhysicalMemory")
- return Convert.ToString(memoriaTotal);
- else
- return str2;
- }
- static void Main(string[] args)
- {
- Console.Title = "Testes com Processamento Paralelo";
- Program programa = new Program();
- programa.Menu();
- }
- static void calculo_Processamento_Paralelo(bool parametro)
- {
- Console.Clear();
- Int64 sequencial; // Armazena o valor do tempo gasto no processamento sequencial
- Int64 paralelo; // Armazena o valor do tempo gasto no processamento paralelo
- int linhas = 1000;
- int colunas = 1000;
- Stopwatch tempo_execucao = new Stopwatch();
- double[,] matriz1 = new double[linhas, colunas];
- double[,] matriz2 = new double[linhas, colunas];
- double[,] matrizResultado = new double[linhas, colunas];
- Random aleatorio = new Random();
- // Geração dos valores das Matrizes
- for (int i = 0; i < linhas; i++)
- {
- for (int j = 0; j < colunas; j++)
- {
- matriz1[i, j] = aleatorio.Next(100);
- matriz2[i, j] = aleatorio.Next(100);
- }
- }
- // Multiplicação dos números das Matrizes de forma sequencial (Loop Não-paralelo)
- tempo_execucao.Start();
- for (int i = 0; i < linhas; i++)
- {
- for (int j = 0; j < colunas; j++)
- {
- matrizResultado[i, j] = matriz1[i, j] * matriz2[i, j];
- if (parametro == true)
- Console.Write("\t" + matrizResultado[i,j]);
- }
- }
- tempo_execucao.Stop();
- TimeSpan contador = tempo_execucao.Elapsed;
- sequencial = contador.Milliseconds;
- tempo_execucao.Reset();
- // Multiplicação dos números das Matrizes de forma paralela (Loop paralelo)
- tempo_execucao.Start();
- Parallel.For(0, linhas, i =>
- {
- for (int j = 0; j < colunas; j++)
- {
- matrizResultado[i, j] = matriz1[i, j] * matriz2[i, j];
- if (parametro == true)
- Console.Write("\t" + matrizResultado[i, j]);
- }
- });
- tempo_execucao.Stop();
- contador = tempo_execucao.Elapsed;
- paralelo = contador.Milliseconds;
- Console.Clear();
- 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);
- }
- public void Menu()
- {
- Console.Clear();
- String variavel;
- string x;
- string y;
- Console.WriteLine("\n\t\t\t - Informações sobre a CPU -\n\n");
- x = ("select Name from Win32_Processor");
- Console.WriteLine("\t\t{0}\n", Busca_Sistema(x));
- x = ("select MaxClockSpeed from Win32_Processor");
- Console.WriteLine("\t\t\tVelocidade da CPU:\t{0}Mhz", Busca_Sistema(x));
- x = ("select NumberOfLogicalProcessors from Win32_Processor");
- y = ("select NumberOfCores from Win32_Processor");
- if (Convert.ToInt16(Busca_Sistema(x)) > Convert.ToInt16(Busca_Sistema(y)))
- y = ("Hypertreading");
- else
- y = ("Turboboost");
- Console.WriteLine("\t\t\tTencologia da Cpu:\t{0}", y);
- x = ("select Architecture from Win32_Processor");
- x = Tipo_Arquitetura(Busca_Sistema(x));
- Console.WriteLine("\t\t\tArquitetura da Cpu:\t{0}", x);
- x = ("select NumberOfCores from Win32_Processor");
- Console.WriteLine("\t\t\tNúmero de Núcleos:\t{0}", Busca_Sistema(x));
- Console.WriteLine("\n\t\t\t :: Escolha um opcão de teste ::");
- 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: ");
- variavel = Console.ReadLine();
- if (variavel == "0")
- {
- Console.Beep();
- return;
- }
- if (variavel == "1")
- {
- calculo_Processamento_Paralelo(true);
- Console.ReadKey();
- Menu();
- }
- if (variavel == "2")
- {
- calculo_Processamento_Paralelo(false);
- Console.ReadKey();
- Menu();
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write("\n\n\tComando invalido. Pressione qualquer tecla pra tentar novamente...");
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.ReadKey();
- Console.Clear();
- Menu();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement