BFS检测CC计算结果(串并行实现)

串行:

package berkstan;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;

public class BerkStanValueCheck {
    public static String COMPARE_SOURCE_RESULTS_DIR =
    	     "D:\\计算结果\\CC-BerkStan\\hash\\improve\\" ;
    public static String SOURCE_RESULTS ="E:\\GTgraph-Data\\BerkStan\\正式测试输入\\BerkStan_Final.txt"  ;
    public static String SOURCE = "source\\";
    public static String IMPROVE = "improve\\";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Integer startId = 1; 
		boolean check = false; 
		HashMap<Integer,Integer> visited = new HashMap<Integer,Integer>();
		Queue<Integer> q=new LinkedList<Integer>();
	    q.add(startId);//将startId作为起始顶点加入队列
	    visited.put(startId, 0) ;
	    while(!q.isEmpty())
	    {
	        Integer top=q.poll();//取出队首元素
	        int d=visited.get(top)+1;//得出其周边还未被访问的节点的距离
	        HashSet<Integer> children = getChildren(top) ;
	        for (Integer c : children) {
	            if(!visited.containsKey(c))//如果visited中还没有该元素说明还没有被访问
	            {
	            	visited.put(c, d);
	            	Integer value = getFileValue(c) ;
	            	if(!value.equals(new Integer(startId))) {  //查找该点对应的improve结果值
	            		System.out.println("第一个不对的:" + c + ", step " + d);
	            		check = true; 
	            		break ;
	            	}
	                q.add(c);
	            } 
	        }
	    }
	    
	    if(check==false) 
	    	System.out.println("Congratulations! No foundings !");
	}

	public  static Integer getFileValue(Integer key) {
		BufferedReader reader = null ;
		Integer value = -1 ;
		
		try {
			for (int p = 0; p < 5; p++) {
				int i = p+1;
				String fileName= COMPARE_SOURCE_RESULTS_DIR + "part-m-0000" + i;
				File File = new File(fileName) ;
				if(File.exists()) {
					reader = new BufferedReader(new FileReader(fileName));
					String tempString = "" ;
					while ((tempString = reader.readLine()) != null)  
					{
						if (!"".equals(tempString)) {
							String[] keyValuesSource = tempString.split("\t");
	                        Integer keySource = Integer.parseInt(keyValuesSource[0]);
	                        
	                        if(!keySource.equals(key))
	                        	continue;
	                        else  {
	                        	value = Integer.parseInt(keyValuesSource[1]);
                                System.out.println(tempString); ;	                        	
	                        }
						}
					}
					reader.close() ;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}catch (Exception e1) {
			e1.printStackTrace();
		}
		
		return value;
	}

	public static HashSet<Integer> getChildren(Integer top) { //取出元素的子节点
		HashSet<Integer> children = new HashSet<Integer>();
		BufferedReader sourceReader = null ;
		
		try {
			File sourceFile = new File(SOURCE_RESULTS) ;
			if(sourceFile.exists()) {
				sourceReader = new BufferedReader(new FileReader(SOURCE_RESULTS));
				String sourceTempString = "" ;
				while ((sourceTempString = sourceReader.readLine()) != null)  
				{
					if (!"".equals(sourceTempString)) {
						String[] keyValuesSource = sourceTempString.split("\t");
                        Integer keySource = Integer.parseInt(keyValuesSource[0]);
                        
                        if(keySource.equals(top)) {
                             for(int i=1 ; i<keyValuesSource.length ; i++){                        	
                            	 children.add(Integer.parseInt(keyValuesSource[i])) ;
                             }
                        }
					}
				}
				sourceReader.close() ;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}catch (Exception e1) {
			e1.printStackTrace();
		}
		
		return children;
	}

}

并行:

package berkstan;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class BerkStanValueCheck {
	public static String COMPARE_SOURCE_RESULTS_DIR = "D:\\计算结果\\CC-BerkStan\\hash\\improve\\";
	public static String SOURCE_RESULTS = "E:\\GTgraph-Data\\BerkStan\\正式测试输入\\BerkStan_Final.txt";
	public static String SOURCE = "source\\";
	public static String IMPROVE = "improve\\";
	public static int MAX_PART_SIZE  = 5 ;
	
	public BerkStanValueCheck() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<Future<Integer>> resultsList = new ArrayList<Future<Integer>>();
		BerkStanValueCheck bvc = new BerkStanValueCheck();
		Integer startId = 1;
		boolean check = false;
		HashMap<Integer, Integer> visited = new HashMap<Integer, Integer>();
		Queue<Integer> q = new LinkedList<Integer>();
		q.add(startId);// 将startId作为起始顶点加入队列
		visited.put(startId, 0);
		while (!q.isEmpty()) {
			Integer top = q.poll();// 取出队首元素
			int d = visited.get(top) + 1;// 得出其周边还未被访问的节点的距离
			HashSet<Integer> children = getChildren(top);
			for (Integer c : children) {
				if (!visited.containsKey(c))// 如果visited中还没有该元素说明还没有被访问
				{
					visited.put(c, d);
					ExecutorService executorService = Executors.newFixedThreadPool(MAX_PART_SIZE);
					for (int i = 1; i <= MAX_PART_SIZE; i++) {
						String fileName = COMPARE_SOURCE_RESULTS_DIR + "part-m-0000" + i;
						resultsList.add(executorService.submit(bvc.new FindValue(fileName, c))) ;
					}

					executorService.shutdown();
					Integer value = -1 ;
					try {
						for (Future<Integer> r : resultsList) {
							Integer result = r.get() ; //get()方法在线程没有结束时会阻塞等待,慎用!
							if (!result.equals(new Integer(-1)))
								value = result ;
							else
								continue;
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}
				 
					if (!value.equals(startId)) { // 查找该点对应的improve结果值
						System.out.println("第一个不对的:" + c + ", step " + d);
						check = true;
						break;
					}
					q.add(c);
				}
			}
		}

		if (check == false)
			System.out.println("Congratulations! No foundings !");
	}

	class FindValue implements Callable<Integer> {
		String fileName = "";
		Integer key = -1;

		public FindValue(String fileName, Integer key) {
			this.fileName = fileName;
			this.key = key;
		}

		@Override
		public Integer call() throws Exception {
			BufferedReader reader = null;
			Integer value = -1;

			try {
				File File = new File(fileName);
				if (File.exists()) {
					reader = new BufferedReader(new FileReader(fileName));
					String tempString = "";
					while ((tempString = reader.readLine()) != null) {
						if (!"".equals(tempString)) {
							String[] keyValuesSource = tempString.split("\t");
							Integer keySource = Integer
									.parseInt(keyValuesSource[0]);

							if (!keySource.equals(key))
								continue;
							else {
								value = Integer.parseInt(keyValuesSource[1]);
								System.out.println(tempString);
							}
						}
					}
					reader.close();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e1) {
				e1.printStackTrace();
			}

			return value;
		}
	}

	public static HashSet<Integer> getChildren(Integer top) { // 取出元素的子节点
		HashSet<Integer> children = new HashSet<Integer>();
		BufferedReader sourceReader = null;

		try {
			File sourceFile = new File(SOURCE_RESULTS);
			if (sourceFile.exists()) {
				sourceReader = new BufferedReader(
						new FileReader(SOURCE_RESULTS));
				String sourceTempString = "";
				while ((sourceTempString = sourceReader.readLine()) != null) {
					if (!"".equals(sourceTempString)) {
						String[] keyValuesSource = sourceTempString.split("\t");
						Integer keySource = Integer
								.parseInt(keyValuesSource[0]);

						if (keySource.equals(top)) {
							for (int i = 1; i < keyValuesSource.length; i++) {
								children.add(Integer
										.parseInt(keyValuesSource[i]));
							}
						}
					}
				}
				sourceReader.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		return children;
	}

}

猜你喜欢

转载自blog.csdn.net/cloudeagle_bupt/article/details/82910798