vcglib/apps/nexus/prefetch.cpp

84 lines
2.0 KiB
C++
Raw Normal View History

2004-12-13 01:44:48 +01:00
#include "prefetch.h"
2004-12-14 15:10:22 +01:00
#include "nexusmt.h"
2004-12-13 01:44:48 +01:00
using namespace std;
using namespace nxs;
void Prefetch::init(NexusMt *m, std::vector<unsigned int> &selected,
std::vector<PServer::Item> &visited) {
2004-12-14 15:10:22 +01:00
// cerr << "Init\n";
2004-12-13 01:44:48 +01:00
safety.lock();
mt = m;
missing.clear();
2004-12-14 15:10:22 +01:00
unsigned int notloaded = 0;
2004-12-13 01:44:48 +01:00
std::map<unsigned int, float> tmp;
for(unsigned int i = 0; i < selected.size(); i++) {
unsigned int patch = selected[i];
tmp[patch] = 0.0f;
2004-12-14 15:10:22 +01:00
if(!mt->patches.IsLoaded(patch))
notloaded++;
// if(mt->patches.IsLoaded(patch))
// mt->todraw.push_back(make_pair(patch, (Patch *)NULL));
// else
2004-12-13 01:44:48 +01:00
missing.push_back(PServer::Item(patch, 0.0f));
}
2004-12-14 15:10:22 +01:00
if(notloaded)
cerr << "Patches to load: " << notloaded << endl;
2004-12-13 01:44:48 +01:00
for(unsigned int i = 0; i < visited.size(); i++) {
PServer::Item &item = visited[i];
if(tmp.count(item.patch)) continue;
if(mt->patches.IsLoaded(item.patch))
tmp[item.patch] = item.priority;
2004-12-14 15:10:22 +01:00
missing.push_back(item);
2004-12-13 01:44:48 +01:00
}
QueuePServer &ps = mt->patches;
for(unsigned int i = 0; i < ps.heap.size(); i++) {
PServer::Item &item = ps.heap[i];
if(tmp.count(item.patch))
item.priority = tmp[item.patch];
else
item.priority = 1e40;
}
make_heap(ps.heap.begin(), ps.heap.end());
2004-12-14 15:10:22 +01:00
2004-12-13 01:44:48 +01:00
sort(missing.begin(), missing.end()); //CRITICAL reverse pero'!
reverse(missing.begin(), missing.end());
safety.unlock();
}
void Prefetch::execute() {
while(1) {
if(get_signaled()) return;
while(1) {
safety.lock();
2004-12-14 15:10:22 +01:00
if(missing.size() == 0) {
safety.unlock();
break;
}
2004-12-13 01:44:48 +01:00
PServer::Item item = missing.back();
missing.pop_back();
2004-12-14 15:10:22 +01:00
if(item.priority > 0 &&
mt->patches.ram_used > mt->patches.ram_max &&
item.priority >= mt->patches.MaxPriority()) {
safety.unlock();
break;
}
2004-12-13 01:44:48 +01:00
PatchInfo &info = mt->index[item.patch];
2004-12-14 15:10:22 +01:00
// cerr << "prefetching: " << item.patch << endl;
2004-12-13 01:44:48 +01:00
mt->patches.Lookup(item.patch, info.nvert, info.nface, item.priority);
2004-12-14 15:10:22 +01:00
2004-12-13 01:44:48 +01:00
safety.unlock();
}
2004-12-14 15:10:22 +01:00
relax(5);
2004-12-13 01:44:48 +01:00
}
}