Commit 189e9254 authored by Ahmad Siavashi's avatar Ahmad Siavashi

finished. untested yet

parent bfeda768
......@@ -143,25 +143,22 @@ public abstract class VgpuScheduler {
}
/**
* Returns pGPUs allocated memories in ascending order.
* Returns the available memory for all on-board pGPUs.
*/
public Map<Pgpu, Integer> getPgpusAllocatedMemory() {
Map<Pgpu, Integer> pgpusAllocatedMemory = new HashMap<>();
public Map<Pgpu, Integer> getPgpusAvailableMemory() {
Map<Pgpu, Integer> pgpusAvailableMemory = new HashMap<>();
for (Pgpu pgpu : getPgpuList()) {
Integer allocatedMemory = 0;
for (Vgpu vgpu : getPgpuVgpuMap().get(pgpu)) {
allocatedMemory += vgpu.getGddram();
}
pgpusAllocatedMemory.put(pgpu, allocatedMemory);
Integer availableMemory = pgpu.getGddramProvisioner().getAvailableGddram();
pgpusAvailableMemory.put(pgpu, availableMemory);
}
return pgpusAllocatedMemory;
return pgpusAvailableMemory;
}
/**
* Returns maximum free memory on GPUs. Assuming all on-board GPUs are the same.
*/
public int getMaxFreeMemory() {
Integer minAllocatedMemory = Collections.min(getPgpusAllocatedMemory().values());
Integer minAllocatedMemory = Collections.min(getPgpusAvailableMemory().values());
int maxFreeMemory = getPgpuList().get(0).getGddramProvisioner().getGddram() - minAllocatedMemory;
return maxFreeMemory;
}
......
......@@ -35,13 +35,13 @@ public abstract class VideoCardAllocationPolicy {
/**
* Return allocated memory of every video card's least loaded GPU.
*/
public Map<VideoCard, Integer> getVideoCardsAllocatedMemory(){
Map<VideoCard, Integer> videoCardsAllocatedMemory = new HashMap<>();
public Map<VideoCard, Integer> getVideoCardsAvailableMemory(){
Map<VideoCard, Integer> videoCardsAvailableMemory = new HashMap<>();
for(VideoCard videoCard : getVideoCards()) {
Integer allocatedMemoryPgpu = Collections.min(videoCard.getVgpuScheduler().getPgpusAllocatedMemory().values());
videoCardsAllocatedMemory.put(videoCard, allocatedMemoryPgpu);
Integer pgpuAvailableMemory = Collections.min(videoCard.getVgpuScheduler().getPgpusAvailableMemory().values());
videoCardsAvailableMemory.put(videoCard, pgpuAvailableMemory);
}
return videoCardsAllocatedMemory;
return videoCardsAvailableMemory;
}
/**
......
......@@ -48,11 +48,11 @@ public class VideoCardAllocationPolicyLeastLoad extends VideoCardAllocationPolic
VideoCard videoCard = Collections.min(candidates, new Comparator<VideoCard>() {
@Override
public int compare(VideoCard videoCard1, VideoCard videoCard2) {
Integer minAllocatedMemoryVideoCard1 = Collections
.min(videoCard1.getVgpuScheduler().getPgpusAllocatedMemory().values());
Integer minAllocatedMemoryVideoCard2 = Collections
.min(videoCard2.getVgpuScheduler().getPgpusAllocatedMemory().values());
return Integer.compare(minAllocatedMemoryVideoCard1, minAllocatedMemoryVideoCard2);
Integer videoCard1maxAvailableMemory = Collections
.max(videoCard1.getVgpuScheduler().getPgpusAvailableMemory().values());
Integer videoCard2maxAvailableMemory = Collections
.max(videoCard2.getVgpuScheduler().getPgpusAvailableMemory().values());
return Integer.compare(videoCard1maxAvailableMemory, videoCard2maxAvailableMemory);
}
});
// Allocate the given vGPU on the selected video card
......
......@@ -9,6 +9,7 @@ import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.gpu.GpuHost;
import org.cloudbus.cloudsim.gpu.GpuVm;
import org.cloudbus.cloudsim.gpu.GpuVmAllocationPolicySimple;
import org.cloudbus.cloudsim.gpu.Vgpu;
......@@ -23,8 +24,8 @@ import org.cloudbus.cloudsim.gpu.power.PowerGpuHost;
*/
public class RemoteGpuVmAllocationPolicyFirstFit extends GpuVmAllocationPolicySimple {
private List<PowerGpuHost> gpuHosts;
private Map<Vgpu, RemoteGpuHost> remoteVgpuHosts;
private List<GpuHost> gpuHostList;
private Map<Vgpu, GpuHost> remoteVgpuHosts;
/**
* This class extends {@link GpuVmAllocationPolicySimple} to add support for GPU
......@@ -35,7 +36,7 @@ public class RemoteGpuVmAllocationPolicyFirstFit extends GpuVmAllocationPolicySi
public RemoteGpuVmAllocationPolicyFirstFit(List<? extends Host> list) {
super(list);
setRemoteVgpuHosts(new HashMap<>());
setGpuHosts(new ArrayList<>());
setGpuHostList(new ArrayList<>());
updateGpuHosts(getHostList());
}
......@@ -56,6 +57,7 @@ public class RemoteGpuVmAllocationPolicyFirstFit extends GpuVmAllocationPolicySi
+ ", 'host': " + host.getId() + "}");
return true;
}
// else if it has a remote vGPU, then
if (allocateRemoteVgpu(vgpu)) {
getVmTable().put(vm.getUid(), host);
Log.formatLine("%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(),
......@@ -73,11 +75,14 @@ public class RemoteGpuVmAllocationPolicyFirstFit extends GpuVmAllocationPolicySi
// Allocates a remote vGPU on a GPU-equipped host
protected boolean allocateRemoteVgpu(Vgpu vgpu) {
// if Vm has a remote Vgpu
for (PowerGpuHost gpuHost : getGpuHosts()) {
for (GpuHost gpuHost : getGpuHostList()) {
boolean isVgpuAllocated = gpuHost.getVideoCardAllocationPolicy().allocate(vgpu, vgpu.getPCIeBw());
getRemoteVgpuHosts().put(vgpu, gpuHost);
if (isVgpuAllocated) {
Log.formatLine("%.2f: Vgpu of VM #" + vgpu.getVm().getId() + " has been allocated to the host #"
+ gpuHost.getId(), CloudSim.clock());
Log.printLine("{'clock': " + CloudSim.clock() + ", 'event': 'vgpu allocation', 'vm': "
+ vgpu.getVm().getId() + ", 'host': " + gpuHost.getId() + "}");
return true;
}
}
......@@ -93,27 +98,38 @@ public class RemoteGpuVmAllocationPolicyFirstFit extends GpuVmAllocationPolicySi
// Add GPU-equipped hosts
protected void updateGpuHosts(List<PowerGpuHost> hosts) {
getGpuHosts().clear();
getGpuHostList().clear();
for (PowerGpuHost host : hosts) {
if (host.getVideoCardAllocationPolicy() != null) {
getGpuHosts().add(host);
getGpuHostList().add(host);
}
}
}
public List<PowerGpuHost> getGpuHosts() {
return gpuHosts;
@Override
public void deallocateHostForVm(Vm vm) {
super.deallocateHostForVm(vm);
Vgpu vgpu = ((GpuVm) vm).getVgpu();
if (vgpu == null || !isVgpuRemote(vgpu)) {
return;
}
getRemoteVgpuHosts().get(vgpu).getVideoCardAllocationPolicy().deallocate(vgpu);
getRemoteVgpuHosts().remove(vgpu);
}
public List<GpuHost> getGpuHostList() {
return gpuHostList;
}
protected void setGpuHosts(List<PowerGpuHost> gpuHosts) {
this.gpuHosts = gpuHosts;
protected void setGpuHostList(List<GpuHost> gpuHosts) {
this.gpuHostList = gpuHosts;
}
public Map<Vgpu, RemoteGpuHost> getRemoteVgpuHosts() {
public Map<Vgpu, GpuHost> getRemoteVgpuHosts() {
return remoteVgpuHosts;
}
protected void setRemoteVgpuHosts(Map<Vgpu, RemoteGpuHost> remoteVgpuHosts) {
protected void setRemoteVgpuHosts(Map<Vgpu, GpuHost> remoteVgpuHosts) {
this.remoteVgpuHosts = remoteVgpuHosts;
}
......
......@@ -37,44 +37,68 @@ public class RemoteGpuVmAllocationPolicyLeastLoad extends RemoteGpuVmAllocationP
@Override
public boolean allocateHostForVm(Vm vm) {
GpuVm gpuVm = (GpuVm) vm;
if (gpuVm.getVgpu() != null) {
sortHosts();
}
return super.allocateHostForVm(vm);
}
@Override
public boolean allocateHostForVm(Vm vm, Host host) {
GpuVm gpuVm = (GpuVm) vm;
if (gpuVm.getVgpu() != null) {
sortHosts();
}
return super.allocateHostForVm(vm, host);
}
protected void sortHosts() {
Collections.sort(getHostList(), new Comparator<GpuHost>() {
@Override
public int compare(GpuHost arg0, GpuHost arg1) {
// TODO
return 0;
Vgpu vgpu = ((GpuVm) vm).getVgpu();
if (vgpu == null) {
for (Host host : getHostList()) {
boolean result = host.vmCreate(vm);
if (result) {
getVmTable().put(vm.getUid(), host);
Log.formatLine("%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(),
CloudSim.clock());
Log.printLine("{'clock': " + CloudSim.clock() + ", 'event': 'vm allocation', 'vm': " + vm.getId()
+ ", 'host': " + host.getId() + "}");
return true;
}
}
});
} else {
// Sort GPU-equipped hosts based on video cards' available memory in descending
// order.
sortGpuHosts();
if (!isVgpuRemote(vgpu)) {
for (Host host : getGpuHostList()) {
boolean result = host.vmCreate(vm);
if (result) {
getVmTable().put(vm.getUid(), host);
Log.formatLine("%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(),
CloudSim.clock());
Log.printLine("{'clock': " + CloudSim.clock() + ", 'event': 'vm allocation', 'vm': "
+ vm.getId() + ", 'host': " + host.getId() + "}");
return true;
}
}
return false;
} else {
for (Host host : getGpuHostList()) {
boolean result = host.vmCreate(vm);
if (result) {
result = allocateRemoteVgpu(vgpu);
if (result) {
getVmTable().put(vm.getUid(), host);
Log.formatLine(
"%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(),
CloudSim.clock());
Log.printLine("{'clock': " + CloudSim.clock() + ", 'event': 'vm allocation', 'vm': "
+ vm.getId() + ", 'host': " + host.getId() + "}");
return true;
}
getRemoteVgpuHosts().get(vgpu).getVideoCardAllocationPolicy().deallocate(vgpu);
}
}
}
}
return false;
}
@Override
protected boolean allocateRemoteVgpu(Vgpu vgpu) {
Collections.sort(getGpuHosts(), new Comparator<PowerGpuHost>() {
public int compare(PowerGpuHost gpuHost1, PowerGpuHost gpuHost2) {
Integer minAllocatedMemoryHost1 = Collections
.min(gpuHost1.getVideoCardAllocationPolicy().getVideoCardsAllocatedMemory().values());
Integer minAllocatedMemoryHost2 = Collections
.min(gpuHost2.getVideoCardAllocationPolicy().getVideoCardsAllocatedMemory().values());
return Integer.compare(minAllocatedMemoryHost1, minAllocatedMemoryHost2);
protected void sortGpuHosts() {
Collections.sort(getGpuHostList(), Collections.reverseOrder(new Comparator<GpuHost>() {
public int compare(GpuHost gpuHost1, GpuHost gpuHost2) {
Integer host1MaxAvailableMemory = Collections
.max(gpuHost1.getVideoCardAllocationPolicy().getVideoCardsAvailableMemory().values());
Integer host2MaxAvailableMemory = Collections
.max(gpuHost2.getVideoCardAllocationPolicy().getVideoCardsAvailableMemory().values());
return Integer.compare(host1MaxAvailableMemory, host2MaxAvailableMemory);
};
});
return super.allocateRemoteVgpu(vgpu);
}));
}
}
/**
*
*/
package org.cloudbus.cloudsim.gpu.selection;
import java.util.Collections;
......@@ -12,7 +9,7 @@ import org.cloudbus.cloudsim.gpu.VgpuScheduler;
/**
* {@link PgpuSelectionPolicyLeastLoad} implements {@link PgpuSelectionPolicy}
* and selects the Pgpu with the least allocated memory.
* and selects the Pgpu with the maximum available memory.
*
* @author Ahmad Siavashi
*
......@@ -37,11 +34,9 @@ public class PgpuSelectionPolicyLeastLoad implements PgpuSelectionPolicy {
return Collections.min(pgpuList, new Comparator<Pgpu>() {
@Override
public int compare(Pgpu pgpu1, Pgpu pgpu2) {
int pgpu1AllocatedMemory = pgpu1.getGddramProvisioner().getGddram()
- pgpu1.getGddramProvisioner().getAvailableGddram();
int pgpu2AllocatedMemory = pgpu2.getGddramProvisioner().getGddram()
- pgpu2.getGddramProvisioner().getAvailableGddram();
return Integer.compare(pgpu1AllocatedMemory, pgpu2AllocatedMemory);
int pgpu1AvailableMemory = pgpu1.getGddramProvisioner().getAvailableGddram();
int pgpu2AvailableMemory = pgpu2.getGddramProvisioner().getAvailableGddram();
return Integer.compare(pgpu1AvailableMemory, pgpu2AvailableMemory);
}
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment