CSP202203-3计算资源调度器

CSP202203-3计算资源调度器

题目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

数据结构图解

在这里插入图片描述

代码

#include<bits/stdc++.h>
using namespace std;
typedef struct node{
	int id;//编号 
	int block;//区
	int nums;//当前的任务数 
	set<int> task; //所运行任务的编号 
	
	bool operator < (const node &a)const{
		if(this->nums!=a.nums){
			return this->nums<a.nums;
		}
		else {
			return this->id<a.id;
		}
	}
}node;

unordered_map<int,node> Node;

//编号为n的可用区内的计算节点
void fax1(int n,vector<int> &temp){
	for(auto it=temp.begin();it!=temp.end();){
		if(Node[*it].block!=n){
			temp.erase(it);
		}
		else it++;
	}
	return;
}

//和编号为n的应用的计算任务在同一个可用区
void fax2(int n,vector<int> &temp){
	set<int> qu; 
	for(auto it=Node.begin();it!=Node.end();it++){
		if(it->second.task.count(n)==1){
			qu.insert(it->second.block);
		}
	}
	for(auto it=temp.begin();it!=temp.end();){
		if(qu.count(Node[*it].block)==0){
			temp.erase(it);
		}
		else it++;
	}
	return;
}

//表示不能和编号为n的应用的计算任务在同一个计算节点上运行
vector<int> fax3(int n,vector<int> temp){
	for(auto it=temp.begin();it!=temp.end();){
		if(Node[*it].task.count(n)==1){
			temp.erase(it);
		}
		else it++;
	}
	return temp;
}

int main(){
#ifdef inline
	freopen("stdin.txt","r",stdin);
#endif
	ios::sync_with_stdio(false);
	int n,m;cin>>n>>m; //n个节点,m个区
	int i=1;
	
	vector<int> idsum;
	while(i<=n){
		int temp;
		cin>>temp;
		Node.insert(pair<int,node>(i,node{i,temp,0,set<int>()}));
		idsum.push_back(i);
		i++;
	}
	int T;cin>>T;
	while(T--){
		int a1,a2,a3,a4,a5,a6;
		cin>>a1>>a2>>a3>>a4>>a5>>a6;
		
// #ifdef inline
// 	for(auto &i:temp){
// 		cout<<i<<" ";
// 	}
// 	cout<<"*************"<<endl;
// #endif
		
		while(a1--){
			vector<int> temp=idsum,temp2;
			//计算节点亲和性
			if(a3!=0) {
				fax1(a3,temp);
			}
			//计算任务亲和性
			if(a4!=0) {
				fax2(a4,temp);
			}
			//计算任务反亲和性
			if(a5!=0) {
				temp2=fax3(a5,temp);
			}
			//计算任务反亲和性要求是必须满足的且无可用节点   或 不考虑 计算任务反亲和性也无可用节点 
			if((temp2.empty()&&a6==1)||temp.empty()) {
				cout<<0<<" ";
				continue;
			}
			//计算任务反亲和性要求是尽量满足的 且 无可用节点   则 不考虑 计算任务反亲和性 
			else if(temp2.empty()&&a6==0){
				temp2=temp;
			}
			//排序 按照节点的已有的作业数、序号 
			sort(temp2.begin(),temp2.end(),[](const auto &a,const auto &b)->bool{
				return Node[a]<Node[b];
			});
			//更新节点信息 
			Node[temp2[0]].nums++;
			Node[temp2[0]].task.insert(a2);
			cout<<Node[temp2[0]].id<<" ";
		}
		cout<<endl;
		
	}
	return 0;
}
CCF(China Computer Federation)CSP(Certified Student Programmer,中国计算机学会认证程序设计)主要是针对青少年和大学生的一门计算机科学竞赛,其中涉及到的计算资源调度问题通常是在算法设计和模拟环境中,比如处理任务分配、并发控制等场景。 关于编写一个简单的计算资源调度器,这里举一个Python的例子,假设我们有任务队列和一个固定数量的处理器: ```python import queue class ResourceScheduler: def __init__(self, num_processors): self.processors = [None] * num_processors self.task_queue = queue.Queue() def add_task(self, task): self.task_queue.put(task) def schedule_tasks(self): while not self.task_queue.empty(): if all(processor is None for processor in self.processors): # 如果所有处理器都空闲,选择一个任务并分配给第一个处理器 current_processor = next((i for i, processor in enumerate(self.processors) if processor is None), None) if current_processor is not None: self.processors[current_processor] = self.task_queue.get() else: # 否则,找到第一个忙碌处理器,并将它的任务移除后分配给下一个处理器 for i, processor in enumerate(self.processors): if processor is not None: completed_task = processor self.processors[i] = None break self.task_queue.put(completed_task) # 使用示例 scheduler = ResourceScheduler(2) scheduler.add_task("Task1") scheduler.add_task("Task2") scheduler.schedule_tasks() ``` 这只是一个基础的简单例子,实际的资源调度器可能会更复杂,考虑到优先级、依赖关系等因素,并使用更高效的数据结构来管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值