locked
急问关于soa并行编程的一个小问题 RRS feed

  • 问题

  • 比如想要实现10000个数字并行加,10个核每个核只算 i * 1000到(i+1)*1000的部分,该怎么弄?在mpi里面有mpi_comm_rank,和mpi_comm_size,但是soa里面在给的文档里面没有找到这样的代码例子。

    2011年10月20日 12:43

答案

  • 这是我写的统计素数的程序,看懂了这个应该就知道怎么做了:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Hpc.Scheduler.Session;
    using PrimesValidation.ServiceReference1;
    using System.ServiceModel;
    using Microsoft.Hpc.Scheduler;
    using System.Diagnostics;
    using System.IO;
    
    namespace PrimesValidation
    {
    	class Program
    	{
    		static readonly string LogPath = Path.Combine(Path.GetDirectoryName(Environment.CommandLine), Environment.GetEnvironmentVariable("CCP_JOBID") + "_output.txt");
    
    		static void Log(string message)
    		{
    			File.AppendAllText(LogPath, message + "\n");
    		}
    
    		static void Main(string[] args)
    		{
    			Log("Program name : " + "PrimesValidation");
    			long startValue = 1;
    			long endValue = 10;
    			int requestsCount = 500;
    
    			Log("Job ID : " + Environment.GetEnvironmentVariable("CCP_JOBID"));
    			Log(string.Format("startValue = {0} endValue = {1} requestsCount = {2}", startValue, endValue, requestsCount));
    
    			SessionStartInfo info = new SessionStartInfo(Environment.GetEnvironmentVariable("CCP_SCHEDULER"), "Contest");
    			info.JobTemplate = "Contest4SOA";
    			
    			Log("Creating session...");
    			try
    			{
    				using (DurableSession session = DurableSession.CreateSession(info))
    				{
    					try
    					{
    						Log("Done.");
    						Log("Session ID = " + session.Id);
    
    						long interval = (endValue - startValue + 1) / requestsCount;
    
    						using (BrokerClient<IService1> client = new BrokerClient<IService1>(session, new NetTcpBinding(SecurityMode.Transport)))
    						{
    							Log("Sending " + requestsCount + " requests...");
    							for (int i = 0; i < requestsCount; ++i)
    							{
    								long start = startValue + interval * i;
    								long end = i + 1 == requestsCount ? endValue : start + interval - 1;
    								Log(string.Format("Request {0}: StartValue = {1}, EndValue = {2}", i, start, end));
    								client.SendRequest<CountPrimesRequest>(new CountPrimesRequest(start, end), i);
    							}
    							Log("Done.");
    							Stopwatch timer = Stopwatch.StartNew();
    							client.EndRequests();
    
    							Log("Retrieving response...");
    							var responses = client.GetResponses<CountPrimesResponse>();
    							long totalCount = 0;
    							foreach (var response in responses)
    							{
    								int requestId = response.GetUserData<int>();
    								long result = response.Result.CountPrimesResult;
    								totalCount += result;
    								Log(string.Format("Response {0}: {1}", requestId, result));
    							}
    							timer.Stop();
    							Log("Done.");
    							Log(string.Format("There are {0} prime numbers in [{1}, {2}].", totalCount, startValue, endValue));
    							Log(string.Format("Time taken: {0}s", timer.Elapsed.TotalSeconds));
    						}
    					}
    					finally
    					{
    						session.Close();
    					}
    				}
    			}
    			catch (Exception ex)
    			{
    				Log(ex.Message);
    			}
    		}
    	}
    }
    
    

    2011年10月20日 13:12

全部回复

  • 这是我写的统计素数的程序,看懂了这个应该就知道怎么做了:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Hpc.Scheduler.Session;
    using PrimesValidation.ServiceReference1;
    using System.ServiceModel;
    using Microsoft.Hpc.Scheduler;
    using System.Diagnostics;
    using System.IO;
    
    namespace PrimesValidation
    {
    	class Program
    	{
    		static readonly string LogPath = Path.Combine(Path.GetDirectoryName(Environment.CommandLine), Environment.GetEnvironmentVariable("CCP_JOBID") + "_output.txt");
    
    		static void Log(string message)
    		{
    			File.AppendAllText(LogPath, message + "\n");
    		}
    
    		static void Main(string[] args)
    		{
    			Log("Program name : " + "PrimesValidation");
    			long startValue = 1;
    			long endValue = 10;
    			int requestsCount = 500;
    
    			Log("Job ID : " + Environment.GetEnvironmentVariable("CCP_JOBID"));
    			Log(string.Format("startValue = {0} endValue = {1} requestsCount = {2}", startValue, endValue, requestsCount));
    
    			SessionStartInfo info = new SessionStartInfo(Environment.GetEnvironmentVariable("CCP_SCHEDULER"), "Contest");
    			info.JobTemplate = "Contest4SOA";
    			
    			Log("Creating session...");
    			try
    			{
    				using (DurableSession session = DurableSession.CreateSession(info))
    				{
    					try
    					{
    						Log("Done.");
    						Log("Session ID = " + session.Id);
    
    						long interval = (endValue - startValue + 1) / requestsCount;
    
    						using (BrokerClient<IService1> client = new BrokerClient<IService1>(session, new NetTcpBinding(SecurityMode.Transport)))
    						{
    							Log("Sending " + requestsCount + " requests...");
    							for (int i = 0; i < requestsCount; ++i)
    							{
    								long start = startValue + interval * i;
    								long end = i + 1 == requestsCount ? endValue : start + interval - 1;
    								Log(string.Format("Request {0}: StartValue = {1}, EndValue = {2}", i, start, end));
    								client.SendRequest<CountPrimesRequest>(new CountPrimesRequest(start, end), i);
    							}
    							Log("Done.");
    							Stopwatch timer = Stopwatch.StartNew();
    							client.EndRequests();
    
    							Log("Retrieving response...");
    							var responses = client.GetResponses<CountPrimesResponse>();
    							long totalCount = 0;
    							foreach (var response in responses)
    							{
    								int requestId = response.GetUserData<int>();
    								long result = response.Result.CountPrimesResult;
    								totalCount += result;
    								Log(string.Format("Response {0}: {1}", requestId, result));
    							}
    							timer.Stop();
    							Log("Done.");
    							Log(string.Format("There are {0} prime numbers in [{1}, {2}].", totalCount, startValue, endValue));
    							Log(string.Format("Time taken: {0}s", timer.Elapsed.TotalSeconds));
    						}
    					}
    					finally
    					{
    						session.Close();
    					}
    				}
    			}
    			catch (Exception ex)
    			{
    				Log(ex.Message);
    			}
    		}
    	}
    }
    
    

    2011年10月20日 13:12
  • 非常感谢!
    2011年10月20日 13:26