一个并行搜索算法

本文介绍了一个基于Java的并行查找算法实现。该算法利用了Java的并发工具包,通过将数组分割为多个子任务并行处理来提高查找效率。文章展示了如何使用`ExecutorService`和`Callable`接口来分配和收集子任务的结果。

抄自《实战Java高并发程序设计》

package understanding;

import java.util.ArrayList;
import java.util.List;
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;
import java.util.concurrent.atomic.AtomicInteger;

//并行查找算法
public class ParallelSearch {

	//要找的数组
	static int[] arr = {1,2,4,5,6,3,4,5,6,7,8};
	
	//cachedThreadPool,按需创建线程池,有就用,没有就创建,过了一段时间还空闲就撤销线程
	//Creates a thread pool that creates new threads as needed, 
	//but will reuse previously constructed threads when they are available. 
	//These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. 
	//Calls to execute will reuse previously constructed threads if available. 
	//If no existing thread is available, a new thread will be created and added to the pool. 
	//Threads that have not been used for sixty seconds are terminated and removed from the cache. 
	//Thus, a pool that remains idle for long enough will not consume any resources. 
	//Note that pools with similar properties but different details (for example, timeout parameters) 
	//may be created using ThreadPoolExecutor constructors.
	static ExecutorService pool = Executors.newCachedThreadPool();
	static final int threadNum = 2;
	
	//存放下标,表示要找的值在这个位置
	static AtomicInteger result = new AtomicInteger(-1);
	
	static class SearchTask implements Callable<Integer>{

		//分别表示开始下标,结束下标,要搜索的值
		int begin,end,searchValue;
		
		//构造方法
		SearchTask(int b,int e,int searchValue){
			this.begin = b;
			this.end = e;
			this.searchValue = searchValue;
		}
		
		@Override
		public Integer call() throws Exception {
			return search(begin, end, searchValue);
		}
		
	}

	//搜索任务要调用的搜索方法,这个方法是关键。
	public static int search(int b,int e,int val){
		
		for(int i=b;i<e;i++){
			if(result.get()>=0){	///已经找到结果了,无需再找,直接返回
				return result.get();
			}
			//没有找到,找找看
			if(arr[i]==val){	//找到了,利用CAS操作设置result的值
				if(!result.compareAndSet(-1, i)){  //设置失败,表示原来的值已经不是-1,说明已经找到了一个结果
					return result.get();
				}
				return i;	//设置成功,返回i
			}
		}
		
		return -1;
	}
	
	public static int pSearch(int searchValue) throws InterruptedException, ExecutionException{
		int subArrSize = arr.length/threadNum+1;
		List<Future<Integer>> re = new ArrayList<Future<Integer>>();
		for(int i=0;i<arr.length;i+=subArrSize){
			int end = i+subArrSize;
			if(end>=arr.length)
				end = arr.length;
			re.add(pool.submit(new SearchTask(i, end, searchValue)));
		}
		for(Future<Integer> futrue:re){
			if(futrue.get()>=0){
				return futrue.get();
			}
		}
		return -1;
	}
	
	public static void main(String[] args) {
		try {
			int res = pSearch(3);
			System.out.println(res);
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}
	}

}


在面对计算昂贵的VLSI布局问题时,设计一个高效的并行搜索算法对于缩短求解时间至关重要。首先,顺序搜索算法,如穷举搜索、回溯法和分支限界法,为问题提供了一个基本的求解框架。穷举搜索方法适用于解空间较小的问题,但在VLSI布局这类大规模问题中,它的计算量是不可接受的。回溯法通过剪枝来减少搜索空间,而分支限界法则通过设定上下界来避免探索无用的分支。这些方法为并行化提供了良好的起点。 参考资源链接:[离散优化算法详解与实现](https://wenku.youkuaiyun.com/doc/1e3wr6stcw?spm=1055.2569.3001.10343) 并行搜索策略的引入是为了解决大规模问题时的计算效率问题。并行深度优先搜索(Parallel DFS)和并行最佳优先搜索(Parallel Best-First Search)是两种主要的并行化方法。Parallel DFS通过将深度优先搜索任务分配给多个处理器来并行执行,能够快速遍历搜索树,尤其是在搜索树高度较大时。Parallel Best-First Search则依赖于一个优先级队列来指导搜索方向,通过并行化可以更高效地探索最有希望的解空间区域。 针对VLSI布局问题,一个有效的并行搜索算法需要考虑以下几点: 1. 确定并行化的粒度,例如是在子树级别还是在单个节点的探索上进行并行化。 2. 设计负载均衡策略,确保所有处理器都有足够的工作量,避免资源浪费。 3. 优化通信机制,减少处理器之间的通信开销,这对于减少算法运行时间至关重要。 4. 考虑如何利用硬件的特性,比如多核CPU、GPU或专用的并行处理单元。 实现这一优化方案的步骤如下: - 使用顺序搜索算法对问题进行初步分析,确定搜索空间的结构特征。 - 基于这些特征,设计并行搜索策略,选择适合并行化的搜索算法。 - 采用负载均衡和通信优化策略,确保算法并行执行时的高效性。 - 利用并行计算硬件资源,执行并行搜索,并监控性能指标以调整算法参数。 通过上述步骤,可以在保证算法正确性的基础上显著提高VLSI布局问题求解的效率。《离散优化算法详解与实现》一书中详细介绍了顺序搜索算法并行搜索算法及其实现,为解决这类问题提供了丰富的理论和实践指导。 参考资源链接:[离散优化算法详解与实现](https://wenku.youkuaiyun.com/doc/1e3wr6stcw?spm=1055.2569.3001.10343)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值