int main(int argc, char* argv[]) { // Main thread for the server // Spawn a new thread for each request po::variables_map vm = parse_opts(argc, argv); debug = vm["debug"].as<bool>(); gpu = vm["gpu"].as<bool>(); Caffe::set_phase(Caffe::TEST); if (vm["gpu"].as<bool>()) Caffe::set_mode(Caffe::GPU); else Caffe::set_mode(Caffe::CPU); // load all models at init ifstream file(vm["nets"].as<string>().c_str()); string net_name; while (file >> net_name) { string net = vm["common"].as<string>() + "configs/" + net_name; Net<float>* temp = new Net<float>(net); const std::string name = temp->name(); nets[name] = temp; std::string weights = vm["common"].as<string>() + vm["weights"].as<string>() + name + ".caffemodel"; nets[name]->CopyTrainedLayersFrom(weights); } // how many threads to spawn before exiting // -1 to stay indefinitely open int total_thread_cnt = vm["threadcnt"].as<int>(); int socketfd = SERVER_init(vm["portno"].as<int>()); // Listen on socket listen(socketfd, 10); LOG(INFO) << "Server is listening for requests on " << vm["portno"].as<int>(); // Main Loop int thread_cnt = 0; while (1) { pthread_t new_thread_id; int client_sock = accept(socketfd, (sockaddr*)0, (unsigned int*)0); if (client_sock == -1) { LOG(ERROR) << "Failed to accept.\n"; continue; } else new_thread_id = request_thread_init(client_sock); ++thread_cnt; if (thread_cnt == total_thread_cnt) { if (pthread_join(new_thread_id, NULL) != 0) { LOG(FATAL) << "Failed to join.\n"; } break; } } return 0; }
EXPORT const char *caffe_net_name(void *netAnon) { Net<float> *net = (Net<float> *)netAnon; return net->name().c_str(); }