23 #ifndef EXATN_NUM_SERVER_HPP_
24 #define EXATN_NUM_SERVER_HPP_
26 #include "tensor_basic.hpp"
27 #include "space_register.hpp"
29 #include "tensor_op_factory.hpp"
30 #include "tensor_network.hpp"
31 #include "tensor_symbol.hpp"
33 #include "tensor_runtime.hpp"
35 #include "Identifiable.hpp"
36 #include "tensor_method.hpp"
37 #include "functor_init_val.hpp"
49 using numerics::VectorSpace;
50 using numerics::Subspace;
51 using numerics::TensorShape;
52 using numerics::TensorSignature;
53 using numerics::TensorLeg;
54 using numerics::Tensor;
55 using numerics::TensorOperation;
56 using numerics::TensorOpFactory;
57 using numerics::TensorNetwork;
59 using TensorMethod = talsh::TensorFunctor<Identifiable>;
75 const std::string & node_executor_name);
82 std::shared_ptr<TensorMethod> method);
89 std::shared_ptr<BytePacket> packet);
96 ScopeId
openScope(
const std::string & scope_name);
120 const std::string & space_name,
121 std::pair<DimOffset,DimOffset> bounds,
122 const Subspace ** subspace_ptr =
nullptr);
134 void submit(std::shared_ptr<TensorOperation> operation);
137 void submit(std::shared_ptr<TensorNetwork> network);
152 bool sync(
const std::string & name,
163 template <
typename... Args>
165 TensorElementType element_type,
168 template <
typename... Args>
169 bool createTensorSync(
const std::string & name,
170 TensorElementType element_type,
176 bool destroyTensorSync(
const std::string & name);
179 template<
typename NumericType>
183 template<
typename NumericType>
184 bool initTensorSync(
const std::string & name,
189 std::shared_ptr<TensorMethod> functor);
191 bool transformTensorSync(
const std::string & name,
192 std::shared_ptr<TensorMethod> functor);
195 template<
typename NumericType>
199 template<
typename NumericType>
200 bool addTensorsSync(
const std::string & addition,
204 template<
typename NumericType>
208 template<
typename NumericType>
209 bool contractTensorsSync(
const std::string & contraction,
214 const std::string & network);
216 bool evaluateTensorNetworkSync(
const std::string & name,
217 const std::string & network);
222 std::shared_ptr<talsh::Tensor>
getLocalTensor(std::shared_ptr<Tensor> tensor,
223 const std::vector<std::pair<DimOffset,DimExtent>> & slice_spec);
225 std::shared_ptr<talsh::Tensor>
getLocalTensor(std::shared_ptr<Tensor> tensor);
227 std::shared_ptr<talsh::Tensor>
getLocalTensor(
const std::string & name,
228 const std::vector<std::pair<DimOffset,DimExtent>> & slice_spec);
230 std::shared_ptr<talsh::Tensor>
getLocalTensor(
const std::string & name);
235 std::unordered_map<std::string,SpaceId> subname2id_;
237 std::unordered_map<std::string,std::shared_ptr<Tensor>> tensors_;
239 std::map<std::string,std::shared_ptr<TensorMethod>> ext_methods_;
240 std::map<std::string,std::shared_ptr<BytePacket>> ext_data_;
242 std::stack<std::pair<std::string,ScopeId>> scopes_;
246 std::shared_ptr<runtime::TensorRuntime> tensor_rt_;
253 template <
typename... Args>
255 TensorElementType element_type,
258 auto res = tensors_.emplace(std::make_pair(name,std::shared_ptr<Tensor>(
new Tensor(name,args...))));
260 std::shared_ptr<TensorOperation> op = tensor_op_factory_->
createTensorOp(TensorOpCode::CREATE);
261 op->setTensorOperand((res.first)->second);
262 std::dynamic_pointer_cast<numerics::TensorOpCreate>(op)->resetTensorElementType(element_type);
265 std::cout <<
"#ERROR(exatn::NumServer::createTensor): Tensor " << name <<
" already exists!" << std::endl;
270 template <
typename... Args>
271 bool NumServer::createTensorSync(
const std::string & name,
272 TensorElementType element_type,
275 auto res = tensors_.emplace(std::make_pair(name,std::shared_ptr<Tensor>(
new Tensor(name,args...))));
277 std::shared_ptr<TensorOperation> op = tensor_op_factory_->
createTensorOp(TensorOpCode::CREATE);
278 op->setTensorOperand((res.first)->second);
279 std::dynamic_pointer_cast<numerics::TensorOpCreate>(op)->resetTensorElementType(element_type);
283 std::cout <<
"#ERROR(exatn::NumServer::createTensor): Tensor " << name <<
" already exists!" << std::endl;
288 template<
typename NumericType>
295 template<
typename NumericType>
296 bool NumServer::initTensorSync(
const std::string & name,
302 template<
typename NumericType>
306 std::vector<std::string> tensors;
309 if(tensors.size() == 2){
310 std::string tensor_name;
311 std::vector<IndexLabel> indices;
313 parsed =
parse_tensor(tensors[0],tensor_name,indices,complex_conj);
315 auto iter = tensors_.find(tensor_name);
316 if(iter != tensors_.end()){
317 auto tensor0 = iter->second;
318 parsed =
parse_tensor(tensors[1],tensor_name,indices,complex_conj);
320 iter = tensors_.find(tensor_name);
321 if(iter != tensors_.end()){
322 auto tensor1 = iter->second;
323 std::shared_ptr<TensorOperation> op = tensor_op_factory_->
createTensorOp(TensorOpCode::ADD);
324 op->setTensorOperand(tensor0);
325 op->setTensorOperand(tensor1);
326 op->setIndexPattern(addition);
327 op->setScalar(0,std::complex<double>(alpha));
331 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Tensor " << tensor_name <<
" not found in tensor addition: "
332 << addition << std::endl;
335 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Invalid argument#1 in tensor addition: "
336 << addition << std::endl;
340 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Tensor " << tensor_name <<
" not found in tensor addition: "
341 << addition << std::endl;
344 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Invalid argument#0 in tensor addition: "
345 << addition << std::endl;
349 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Invalid number of arguments in tensor addition: "
350 << addition << std::endl;
353 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Invalid tensor addition: " << addition << std::endl;
358 template<
typename NumericType>
359 bool NumServer::addTensorsSync(
const std::string & addition,
362 std::vector<std::string> tensors;
365 if(tensors.size() == 2){
366 std::string tensor_name;
367 std::vector<IndexLabel> indices;
369 parsed =
parse_tensor(tensors[0],tensor_name,indices,complex_conj);
371 auto iter = tensors_.find(tensor_name);
372 if(iter != tensors_.end()){
373 auto tensor0 = iter->second;
374 parsed =
parse_tensor(tensors[1],tensor_name,indices,complex_conj);
376 iter = tensors_.find(tensor_name);
377 if(iter != tensors_.end()){
378 auto tensor1 = iter->second;
379 std::shared_ptr<TensorOperation> op = tensor_op_factory_->
createTensorOp(TensorOpCode::ADD);
380 op->setTensorOperand(tensor0);
381 op->setTensorOperand(tensor1);
382 op->setIndexPattern(addition);
383 op->setScalar(0,std::complex<double>(alpha));
388 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Tensor " << tensor_name <<
" not found in tensor addition: "
389 << addition << std::endl;
392 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Invalid argument#1 in tensor addition: "
393 << addition << std::endl;
397 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Tensor " << tensor_name <<
" not found in tensor addition: "
398 << addition << std::endl;
401 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Invalid argument#0 in tensor addition: "
402 << addition << std::endl;
406 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Invalid number of arguments in tensor addition: "
407 << addition << std::endl;
410 std::cout <<
"#ERROR(exatn::NumServer::addTensors): Invalid tensor addition: " << addition << std::endl;
415 template<
typename NumericType>
419 std::vector<std::string> tensors;
422 if(tensors.size() == 3){
423 std::string tensor_name;
424 std::vector<IndexLabel> indices;
426 parsed =
parse_tensor(tensors[0],tensor_name,indices,complex_conj);
428 auto iter = tensors_.find(tensor_name);
429 if(iter != tensors_.end()){
430 auto tensor0 = iter->second;
431 parsed =
parse_tensor(tensors[1],tensor_name,indices,complex_conj);
433 iter = tensors_.find(tensor_name);
434 if(iter != tensors_.end()){
435 auto tensor1 = iter->second;
436 parsed =
parse_tensor(tensors[2],tensor_name,indices,complex_conj);
438 iter = tensors_.find(tensor_name);
439 if(iter != tensors_.end()){
440 auto tensor2 = iter->second;
441 std::shared_ptr<TensorOperation> op = tensor_op_factory_->
createTensorOp(TensorOpCode::CONTRACT);
442 op->setTensorOperand(tensor0);
443 op->setTensorOperand(tensor1);
444 op->setTensorOperand(tensor2);
445 op->setIndexPattern(contraction);
446 op->setScalar(0,std::complex<double>(alpha));
450 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Tensor " << tensor_name <<
" not found in tensor contraction: "
451 << contraction << std::endl;
454 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Invalid argument#2 in tensor contraction: "
455 << contraction << std::endl;
459 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Tensor " << tensor_name <<
" not found in tensor contraction: "
460 << contraction << std::endl;
463 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Invalid argument#1 in tensor contraction: "
464 << contraction << std::endl;
468 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Tensor " << tensor_name <<
" not found in tensor contraction: "
469 << contraction << std::endl;
472 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Invalid argument#0 in tensor contraction: "
473 << contraction << std::endl;
477 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Invalid number of arguments in tensor contraction: "
478 << contraction << std::endl;
481 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Invalid tensor contraction: " << contraction << std::endl;
486 template<
typename NumericType>
487 bool NumServer::contractTensorsSync(
const std::string & contraction,
490 std::vector<std::string> tensors;
493 if(tensors.size() == 3){
494 std::string tensor_name;
495 std::vector<IndexLabel> indices;
497 parsed =
parse_tensor(tensors[0],tensor_name,indices,complex_conj);
499 auto iter = tensors_.find(tensor_name);
500 if(iter != tensors_.end()){
501 auto tensor0 = iter->second;
502 parsed =
parse_tensor(tensors[1],tensor_name,indices,complex_conj);
504 iter = tensors_.find(tensor_name);
505 if(iter != tensors_.end()){
506 auto tensor1 = iter->second;
507 parsed =
parse_tensor(tensors[2],tensor_name,indices,complex_conj);
509 iter = tensors_.find(tensor_name);
510 if(iter != tensors_.end()){
511 auto tensor2 = iter->second;
512 std::shared_ptr<TensorOperation> op = tensor_op_factory_->
createTensorOp(TensorOpCode::CONTRACT);
513 op->setTensorOperand(tensor0);
514 op->setTensorOperand(tensor1);
515 op->setTensorOperand(tensor2);
516 op->setIndexPattern(contraction);
517 op->setScalar(0,std::complex<double>(alpha));
522 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Tensor " << tensor_name <<
" not found in tensor contraction: "
523 << contraction << std::endl;
526 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Invalid argument#2 in tensor contraction: "
527 << contraction << std::endl;
531 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Tensor " << tensor_name <<
" not found in tensor contraction: "
532 << contraction << std::endl;
535 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Invalid argument#1 in tensor contraction: "
536 << contraction << std::endl;
540 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Tensor " << tensor_name <<
" not found in tensor contraction: "
541 << contraction << std::endl;
544 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Invalid argument#0 in tensor contraction: "
545 << contraction << std::endl;
549 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Invalid number of arguments in tensor contraction: "
550 << contraction << std::endl;
553 std::cout <<
"#ERROR(exatn::NumServer::contractTensors): Invalid tensor contraction: " << contraction << std::endl;
560 #endif //EXATN_NUM_SERVER_HPP_