1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
extern crate proc_macro;
mod am_data;
mod parse;
mod replace;
mod array_ops;
mod array_reduce;
mod gen_am;
mod gen_am_group;
mod field_info;
use am_data::derive_am_data;
use proc_macro::TokenStream;
use proc_macro_error::{abort, emit_error, proc_macro_error};
use quote::{quote, quote_spanned, ToTokens};
use syn::parse_macro_input;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
// use syn::Meta;
// use syn::visit_mut::VisitMut;
use syn::parse::{Parse, ParseStream, Result};
use syn::Token;
fn type_name(ty: &syn::Type) -> Option<String> {
match ty {
syn::Type::Path(syn::TypePath { qself: None, path }) => {
Some(path.segments.last().unwrap().ident.to_string())
}
_ => None,
}
}
#[allow(dead_code)]
fn get_impl_associated_type(name: String, tys: &Vec<syn::ImplItem>) -> Option<syn::Type> {
for ty in tys {
match ty {
syn::ImplItem::Type(ref item) => {
if item.ident.to_string() == name {
return Some(item.ty.clone());
}
}
_ => (),
}
}
None
}
fn get_return_of_method(name: String, tys: &Vec<syn::ImplItem>) -> Option<syn::Type> {
for ty in tys {
match ty {
syn::ImplItem::Fn(ref item) => {
if item.sig.asyncness.is_some() {
if item.sig.ident.to_string() == name {
match item.sig.output.clone() {
syn::ReturnType::Default => {
return None;
}
syn::ReturnType::Type(_, item) => {
return Some(*item);
}
}
}
} else {
abort!(item.sig.fn_token.span(),"implementing lamellar::am expects the exec function to be async (e.g. 'async fn exec(...)')")
}
}
_ => (),
}
}
None
}
fn get_impl_method(name: String, tys: &Vec<syn::ImplItem>) -> Option<syn::Block> {
for ty in tys {
match ty {
syn::ImplItem::Fn(ref item) => {
if item.sig.ident.to_string() == name {
return Some(item.block.clone());
}
}
_ => (),
}
}
None
}
fn get_expr(stmt: &syn::Stmt) -> Option<syn::Expr> {
let expr = match stmt {
syn::Stmt::Expr(expr, semi) => match expr.clone() {
syn::Expr::Return(expr) => Some(*(expr.expr.unwrap())),
_ => {
if semi.is_some() {
None
} else {
Some(expr.clone())
}
}
},
syn::Stmt::Macro(mac) => {
abort!(mac.span(),"we currently do not support macros in return position, assign macro output to a variable, and return the variable");
}
_ => {
println!("something else!");
None
}
};
expr
}
#[derive(Clone)]
enum AmType {
NoReturn,
ReturnData(syn::Type),
ReturnAm(syn::Type, proc_macro2::TokenStream),
}
fn get_return_am_return_type(
args: &Punctuated<syn::Meta, syn::Token![,]>,
) -> Option<(proc_macro2::TokenStream, proc_macro2::TokenStream)> {
for arg in args.iter() {
let arg_str = arg.to_token_stream().to_string();
if arg_str.contains("return_am") {
let mut the_am = arg_str
.split("return_am")
.collect::<Vec<&str>>()
.last()
.expect("error in lamellar::am argument")
.trim_matches(&['=', ' ', '"'][..])
.to_string();
let mut return_type = "".to_string();
if the_am.find("->") != None {
let temp = the_am.split("->").collect::<Vec<&str>>();
return_type = temp
.last()
.expect("error in lamellar::am argument")
.trim()
.to_string();
the_am = temp[0].trim_matches(&[' ', '"'][..]).to_string();
}
let ret_am_type: syn::Type = syn::parse_str(&the_am).expect("invalid type");
if return_type.len() > 0 {
// let ident = syn::Ident::new(&return_type, Span::call_site());
let ret_type: syn::Type = syn::parse_str(&return_type).expect("invalid type");
return Some((
quote_spanned! {arg.span() => #ret_am_type},
quote_spanned! {arg.span() => #ret_type},
));
} else {
return Some((quote! {#ret_am_type}, quote! {()}));
}
}
}
None
}
fn check_for_am_group(args: &Punctuated<syn::Meta, syn::Token![,]>) -> bool {
for arg in args.iter() {
let t = arg.to_token_stream().to_string();
if t.contains("AmGroup") {
if t.contains("(") {
let attrs = &t[t.find("(").unwrap()
..t.find(")")
.expect("missing \")\" in when declaring ArrayOp macro")
+ 1];
if attrs.contains("false") {
return false;
}
}
}
}
true
}
/// # Examples
///
///```
/// use lamellar::active_messaging::prelude::*;
/// use lamellar::darc::prelude*;
///
/// #[AmData(Debug,Clone)]
/// struct HelloWorld {
/// originial_pe: usize,
/// #[AmData(static)]
/// msg: Darc<String>,
/// }
///
/// #[lamellar::am]
/// impl LamellarAM for HelloWorld {
/// async fn exec(self) {
/// println!(
/// "{:?}} on PE {:?} of {:?} using thread {:?}, received from PE {:?}",
/// self.msg,
/// lamellar::current_pe,
/// lamellar::num_pes,
/// std::thread::current().id(),
/// self.originial_pe.lock(),
/// );
/// }
/// }
/// fn main() {
/// let world = lamellar::LamellarWorldBuilder::new().build();
/// let my_pe = world.my_pe();
/// world.barrier();
/// let msg = Darc::<String>::new(&world, "Hello World".to_string());
/// //Send a Hello World Active Message to all pes
/// let request = world.exec_am_all(HelloWorld {
/// originial_pe: my_pe,
/// msg: msg,
/// });
///
/// //wait for the request to complete
/// world.block_on(request);
/// } //when world drops there is an implicit world.barrier() that occurs
///```
#[allow(non_snake_case)]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn AmData(args: TokenStream, input: TokenStream) -> TokenStream {
let args =
parse_macro_input!(args with Punctuated<syn::Meta, syn::Token![,]>::parse_terminated);
// println!("here");
derive_am_data(input, args, quote! {__lamellar}, false, false, false)
}
///```
/// use lamellar::active_messaging::prelude::*;
///
/// #[AmLocalData(Debug,Clone)]
/// struct HelloWorld {
/// originial_pe: Arc<Mutex<usize>>, //This would not be allowed in a non-local AM as Arc<Mutex<<>> is not (de)serializable
/// }
///
/// #[lamellar::local_am]
/// impl LamellarAM for HelloWorld {
/// async fn exec(self) {
/// println!(
/// "Hello World on PE {:?} of {:?} using thread {:?}, received from PE {:?}",
/// lamellar::current_pe,
/// lamellar::num_pes,
/// std::thread::current().id(),
/// self.originial_pe.lock(),
/// );
/// }
/// }
/// fn main() {
/// let world = lamellar::LamellarWorldBuilder::new().build();
/// let my_pe = Arc::new(Mutex::new(world.my_pe()));
/// world.barrier();
///
/// let request = world.exec_am_local(HelloWorld {
/// originial_pe: my_pe,
/// });
///
/// //wait for the request to complete
/// world.block_on(request);
/// } //when world drops there is an implicit world.barrier() that occurs
///```
#[allow(non_snake_case)]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn AmLocalData(args: TokenStream, input: TokenStream) -> TokenStream {
let args =
parse_macro_input!(args with Punctuated<syn::Meta, syn::Token![,]>::parse_terminated);
derive_am_data(input, args, quote! {__lamellar}, true, false, false)
}
#[allow(non_snake_case)]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn AmGroupData(args: TokenStream, input: TokenStream) -> TokenStream {
let args =
parse_macro_input!(args with Punctuated<syn::Meta, syn::Token![,]>::parse_terminated);
derive_am_data(input, args, quote! {__lamellar}, false, true, false)
}
#[doc(hidden)]
#[allow(non_snake_case)]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn AmDataRT(args: TokenStream, input: TokenStream) -> TokenStream {
let args =
parse_macro_input!(args with Punctuated<syn::Meta, syn::Token![,]>::parse_terminated);
derive_am_data(input, args, quote! {crate}, false, false, true)
}
#[doc(hidden)]
#[allow(non_snake_case)]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn AmLocalDataRT(args: TokenStream, input: TokenStream) -> TokenStream {
let args =
parse_macro_input!(args with Punctuated<syn::Meta, syn::Token![,]>::parse_terminated);
derive_am_data(input, args, quote! {crate}, true, false, true)
}
fn parse_am(
args: TokenStream,
input: TokenStream,
local: bool,
rt: bool,
_am_group: bool,
) -> TokenStream {
let args =
parse_macro_input!(args with Punctuated<syn::Meta, syn::Token![,]>::parse_terminated);
// let args = args.to_string();
// if args.len() > 0 {
// if !args.starts_with("return_am") {
// abort!(args.span(),"#[lamellar::am] only accepts an (optional) argument of the form:
// #[lamellar::am(return_am = \"<am to exec upon return>\")]");
// }
// }
// println!("args: {:?}", args);
let input: syn::Item = parse_macro_input!(input);
let lamellar = if rt {
// quote::format_ident!("crate")
quote! {crate}
} else {
// quote::format_ident!("__lamellar")
quote! {__lamellar}
};
let am_data_header = if rt {
if !local {
quote! {#[lamellar_impl::AmDataRT]}
} else {
quote! {#[lamellar_impl::AmLocalDataRT]}
}
} else {
if !local {
quote! {#[#lamellar::AmData]}
} else {
quote! {#[#lamellar::AmLocalData]}
}
};
let am_group_data_header = quote! {#[#lamellar::AmGroupData]};
let create_am_group = check_for_am_group(&args);
let output = match input.clone() {
syn::Item::Impl(input) => {
let output = get_return_of_method("exec".to_string(), &input.items);
match output {
Some(output) => {
if let Some((return_am, return_output)) = get_return_am_return_type(&args) {
if return_am.to_string() != output.to_token_stream().to_string() {
emit_error!(
return_am.span(),
"am specified in attribute {} does not match return type {}",
return_am,
output.to_token_stream().to_string()
);
abort!(
output.span(),
"am specified in attribute {} does not match return type {}",
return_am,
output.to_token_stream().to_string()
);
}
let mut impls = gen_am::generate_am(
&input,
local,
AmType::ReturnAm(output.clone(), return_output.clone()),
&lamellar,
&am_data_header,
);
if !rt && !local && create_am_group {
impls.extend(gen_am_group::generate_am_group(
&input,
local,
AmType::ReturnAm(output.clone(), return_output.clone()),
&lamellar,
&am_group_data_header,
));
}
impls
} else {
let mut impls = gen_am::generate_am(
&input,
local,
AmType::ReturnData(output.clone()),
&lamellar,
&am_data_header,
);
if !rt && !local && create_am_group {
impls.extend(gen_am_group::generate_am_group(
&input,
local,
AmType::ReturnData(output.clone()),
&lamellar,
&am_group_data_header,
));
}
impls
}
}
None => {
let mut impls = gen_am::generate_am(
&input,
local,
AmType::NoReturn,
&lamellar,
&am_data_header,
);
if !rt && !local && create_am_group {
impls.extend(gen_am_group::generate_am_group(
&input,
local,
AmType::NoReturn,
&lamellar,
&am_group_data_header,
));
}
impls
}
}
}
_ => {
println!("lamellar am attribute only valid for impl blocks");
let output = quote! { #input };
output.into()
}
};
output
}
/// # Examples
///
///```
/// use lamellar::active_messaging::prelude::*;
/// use lamellar::darc::prelude::*;
///
/// #[AmData(Debug,Clone)]
/// struct HelloWorld {
/// originial_pe: usize,
/// #[AmData(static)]
/// msg: Darc<String>,
/// }
///
/// #[lamellar::am]
/// impl LamellarAM for HelloWorld {
/// async fn exec(self) {
/// println!(
/// "{:?}} on PE {:?} of {:?} using thread {:?}, received from PE {:?}",
/// self.msg,
/// lamellar::current_pe,
/// lamellar::num_pes,
/// std::thread::current().id(),
/// self.originial_pe.lock(),
/// );
/// }
/// }
/// fn main() {
/// let world = lamellar::LamellarWorldBuilder::new().build();
/// let my_pe = world.my_pe();
/// world.barrier();
/// let msg = Darc::<String>::new(&world, "Hello World".to_string());
/// //Send a Hello World Active Message to all pes
/// let request = world.exec_am_all(HelloWorld {
/// originial_pe: my_pe,
/// msg: msg,
/// });
///
/// //wait for the request to complete
/// world.block_on(request);
/// } //when world drops there is an implicit world.barrier() that occurs
///```
#[proc_macro_error]
#[proc_macro_attribute]
pub fn am(args: TokenStream, input: TokenStream) -> TokenStream {
parse_am(args, input, false, false, true)
}
#[doc(hidden)]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn am_group(args: TokenStream, input: TokenStream) -> TokenStream {
parse_am(args, input, false, false, false)
}
/// # Examples
///
///```
/// use lamellar::active_messaging::prelude::*;
///
/// #[AmLocalData(Debug,Clone)]
/// struct HelloWorld {
/// originial_pe: Arc<Mutex<usize>>, //This would not be allowed in a non-local AM as Arc<Mutex<<>> is not (de)serializable
/// }
///
/// #[lamellar::local_am]
/// impl LamellarAM for HelloWorld {
/// async fn exec(self) {
/// println!(
/// "Hello World on PE {:?} of {:?} using thread {:?}, received from PE {:?}",
/// lamellar::current_pe,
/// lamellar::num_pes,
/// std::thread::current().id(),
/// self.originial_pe.lock(),
/// );
/// }
/// }
/// fn main() {
/// let world = lamellar::LamellarWorldBuilder::new().build();
/// let my_pe = Arc::new(Mutex::new(world.my_pe()));
/// world.barrier();
///
/// let request = world.exec_am_local(HelloWorld {
/// originial_pe: my_pe,
/// });
///
/// //wait for the request to complete
/// world.block_on(request);
/// } //when world drops there is an implicit world.barrier() that occurs
///```
#[proc_macro_error]
#[proc_macro_attribute]
pub fn local_am(args: TokenStream, input: TokenStream) -> TokenStream {
parse_am(args, input, true, false, false)
}
#[doc(hidden)]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn rt_am(args: TokenStream, input: TokenStream) -> TokenStream {
parse_am(args, input, false, true, false)
}
#[doc(hidden)]
#[proc_macro_error]
#[proc_macro_attribute]
pub fn rt_am_local(args: TokenStream, input: TokenStream) -> TokenStream {
parse_am(args, input, true, true, false)
}
#[doc(hidden)]
#[proc_macro_error]
#[proc_macro_derive(Dist)]
pub fn derive_dist(input: TokenStream) -> TokenStream {
let mut output = quote! {};
let input = parse_macro_input!(input as syn::DeriveInput);
let name = input.ident;
output.extend(quote! {
const _: () = {
extern crate lamellar as __lamellar;
impl __lamellar::Dist for #name {}
};
});
// output.extend(create_ops(name.clone(), &write_array_types, false, false));
TokenStream::from(output)
}
#[proc_macro_error]
#[proc_macro]
pub fn register_reduction(item: TokenStream) -> TokenStream {
array_reduce::__register_reduction(item)
}
// probalby should turn this into a derive macro
// #[proc_macro_error]
// #[proc_macro]
// pub fn generate_reductions_for_type(item: TokenStream) -> TokenStream {
// array_reduce::__generate_reductions_for_type(item)
// }
#[doc(hidden)]
#[proc_macro_error]
#[proc_macro]
pub fn generate_reductions_for_type_rt(item: TokenStream) -> TokenStream {
array_reduce::__generate_reductions_for_type_rt(item)
}
// / This macro automatically implements various LamellarArray "Op" traits for user defined types
// /
// / The following "Op" traits will be implemented:
// / - [AccessOps][lamellar::array::AccessOps]
// / - [ArithmeticOps][lamellar::array::AccessOps]
// / - [BitWiseOps][lamellar::array::AccessOps]
// / - [CompareExchangeEpsilonOps][lamellar::array::AccessOps]
// / - [CompareExchangeOps][lamellar::array::AccessOps]
// /
// / The required trait bounds can be found by viewing each "Op" traits documentation.
// / Generally though the type must be able to be used in an active message,
// / # Examples
// /
// /```
// / use lamellar::array::prelude::*;
// #[proc_macro_error]
// #[proc_macro]
// pub fn generate_ops_for_type(item: TokenStream) -> TokenStream {
// array_ops::__generate_ops_for_type(item)
// }
#[doc(hidden)]
#[proc_macro_error]
#[proc_macro]
pub fn generate_ops_for_type_rt(item: TokenStream) -> TokenStream {
array_ops::__generate_ops_for_type_rt(item)
}
#[doc(hidden)]
#[proc_macro_error]
#[proc_macro]
pub fn generate_ops_for_bool_rt(_item: TokenStream) -> TokenStream {
array_ops::__generate_ops_for_bool_rt()
}
///
/// This derive macro is intended to be used with the [macro@AmData] attribute macro to enable a user defined type to be used in ActiveMessages.
///
/// # Examples
///
/// ```
/// // this import includes everything we need
/// use lamellar::array::prelude::*;
///
///
/// #[lamellar::AmData(
/// // Lamellar traits
/// ArrayOps(Arithmetic,CompExEps,Shift), // needed to derive various LamellarArray Op traits (provided as a list)
/// Default, // needed to be able to initialize a LamellarArray
/// // Notice we use `lamellar::AmData` instead of `derive`
/// // for common traits, e.g. Debug, Clone.
/// PartialEq, // needed for CompareExchangeEpsilonOps
/// PartialOrd, // needed for CompareExchangeEpsilonOps
/// Debug, // any addition traits you want derived
/// Clone,
/// )]
/// struct Custom {
/// int: usize,
/// float: f32,
/// }
///
/// // We need to impl various arithmetic ops if we want to be able to
/// // perform remote arithmetic operations with this type
/// impl std::ops::AddAssign for Custom {
/// fn add_assign(&mut self, other: Self) {
/// *self = Self {
/// int: self.int + other.int,
/// float: self.float + other.float,
/// }
/// }
/// }
///
/// impl std::ops::SubAssign for Custom {
/// fn sub_assign(&mut self, other: Self) {
/// *self = Self {
/// int: self.int - other.int,
/// float: self.float - other.float,
/// }
/// }
/// }
///
/// impl std::ops::Sub for Custom {
/// type Output = Self;
/// fn sub(self, other: Self) -> Self {
/// Self {
/// int: self.int - other.int,
/// float: self.float - other.float,
/// }
/// }
/// }
///
/// impl std::ops::MulAssign for Custom {
/// fn mul_assign(&mut self, other: Self) {
/// *self = Self {
/// int: self.int * other.int,
/// float: self.float * other.float,
/// }
/// }
/// }
///
/// impl std::ops::DivAssign for Custom {
/// fn div_assign(&mut self, other: Self) {
/// *self = Self {
/// int: self.int / other.int,
/// float: self.float / other.float,
/// }
/// }
/// }
/// impl std::ops::ShlAssign for Custom {
/// fn shl_assign(&mut self,other: Custom){
/// self.int <<= other.int;
/// }
/// }
///
/// impl std::ops::ShrAssign for Custom {
/// fn shr_assign(&mut self,other: Custom){
/// self.int >>= other.int;
/// }
/// }
///
/// fn main(){
///
/// // initialize
/// // -----------
///
/// let world = LamellarWorldBuilder::new().build(); // the world
///
/// let array = // the atomic distributed array
/// AtomicArray::<Custom>::new(&world,3,Distribution::Block);
///
/// println!();
/// println!("initialize a length-3 array:\n"); // print the entries
/// array.dist_iter()
/// .enumerate()
/// .for_each(|(i,entry)| println!("entry {:?}: {:?}", i, entry ) );
/// array.wait_all();
///
/// // call various operations on the array!
/// // -------------------------------------
///
/// world.block_on( async move { // we will just use the world as our future driver so we dont have to deal with cloneing array
///
/// println!();
/// println!("add (1, 0.01) to the first entry:\n");
/// let val = Custom{int: 1, float: 0.01};
/// array.add(0, val ).await;
/// array.dist_iter().enumerate().for_each(|(i,entry)| println!("entry {:?}: {:?}", i, entry ) );
/// array.wait_all();
///
/// println!();
/// println!("batch compare/exchange:");
/// let indices = vec![0,1,2,];
/// let current = val;
/// let new = Custom{int: 1, float: 0.0};
/// let epsilon = Custom{int: 0, float: 0.01};
/// let _results = array.batch_compare_exchange_epsilon(indices,current,new,epsilon).await;
/// println!();
/// println!("(1) the updatd array");
/// array.dist_iter().enumerate().for_each(|(i,entry)| println!("entry {:?}: {:?}", i, entry ) );
/// array.wait_all();
/// println!();
/// println!("(2) the return values");
/// for (i, entry) in _results.iter().enumerate() { println!("entry {:?}: {:?}", i, entry ) }
/// });
///
/// // inspect the results
/// // -------------------------------------
/// // NB: because thewe're working with multithreaded async
/// // environments, entries may be printed out of order
/// //
/// // initialize a length-3 array:
/// //
/// // entry 1: Custom { int: 0, float: 0.0 }
/// // entry 0: Custom { int: 0, float: 0.0 }
/// // entry 2: Custom { int: 0, float: 0.0 }
/// //
/// // add (1, 0.01) to the first entry:
/// //
/// // entry 0: Custom { int: 1, float: 0.01 }
/// // entry 2: Custom { int: 0, float: 0.0 }
/// // entry 1: Custom { int: 0, float: 0.0 }
/// //
/// // batch compare/exchange:
/// //
/// // (1) the updatd array
/// // entry 0: Custom { int: 1, float: 0.0 }
/// // entry 1: Custom { int: 0, float: 0.0 }
/// // entry 2: Custom { int: 0, float: 0.0 }
/// //
/// // (2) the return values
/// // entry 0: Ok(Custom { int: 1, float: 0.01 })
/// // entry 1: Err(Custom { int: 0, float: 0.0 })
/// // entry 2: Err(Custom { int: 0, float: 0.0 })
/// }
/// ```
#[proc_macro_error]
#[proc_macro_derive(ArrayOps, attributes(array_ops))]
pub fn derive_arrayops(input: TokenStream) -> TokenStream {
array_ops::__derive_arrayops(input)
}
struct AmGroups {
am: syn::TypePath,
team: syn::Expr,
}
impl Parse for AmGroups {
fn parse(input: ParseStream) -> Result<Self> {
// println!("in am groups parse");
let am = if let Ok(syn::Type::Path(ty)) = input.parse() {
ty.clone()
} else {
abort!(input.span(),"typed_am_group expects the first argument to be Struct name if your active message e.g.
#[AmData]
Struct MyAmStruct {}
...
typed_am_group!(MyAmStruct,...)");
};
// println!("am: {:?}",am);
input.parse::<Token![,]>()?;
let team_error_msg = "typed_am_group expects a LamellarWorld or LamellarTeam instance as it's only argument e.g.
'typed_am_group!(...,&world)',
'typed_am_group!(...,world.clone())'
'typed_am_group!(...,&team)',
'typed_am_group!(...,team.clone())'";
let team = if let Ok(expr) = input.parse::<syn::Expr>() {
match expr {
syn::Expr::Path(_) => expr.clone(),
syn::Expr::Reference(_) => expr.clone(),
syn::Expr::MethodCall(_) => expr.clone(),
_ => abort!(input.span(), team_error_msg),
}
} else {
abort!(input.span(), team_error_msg);
};
Ok(AmGroups { am, team })
}
}
/// The macro used to create an new instance of a `TypedAmGroup` which is an Active Message Group that can only include AMs of a specific type (but this type can return data).
/// Data is returned in the same order as the AMs were added
/// (You can think of this as similar to `Vec<T>`)
/// This macro which expects two parameters, the first being the type (name) of the AM and the second being a reference to a lamellar team.
/// ```
/// use lamellar::active_messaging::prelude::*;
/// use lamellar::darc::prelude::*;
/// use std::sync::atomic::AtomicUsize;
/// #[AmData(Debug,Clone)]
/// struct ExampleAm {
/// cnt: Darc<AtomicUsize>,
/// }
/// #[lamellar::am]
/// impl LamellarAm for ExampleAm{
/// async fn exec(self) -> usize{
/// self.cnt.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
/// }
/// }
///
/// fn main(){
/// let world = lamellar::LamellarWorldBuilder::new().build();
/// let my_pe = world.my_pe();
/// let num_pes = world.num_pes();
///
/// if my_pe == 0 { // we only want to run this on PE0 for sake of illustration
/// let am_group = typed_am_group!{ExampleAm,&world};
/// let am = ExampleAm{cnt: 0};
/// // add the AMs to the group
/// // we can specify individual PEs to execute on or all PEs
/// am_group.add_am_pe(0,am.clone());
/// am_group.add_am_all(am.clone());
/// am_group.add_am_pe(1,am.clone());
/// am_group.add_am_all(am.clone());
///
/// //execute and await the completion of all AMs in the group
/// let results = world.block_on(am_group.exec()); // we want to process the returned data
/// //we can index into the results
/// if let AmGroupResult::Pe((pe,val)) = results.at(2){
/// assert_eq!(pe, 1); //the third add_am_* call in the group was to execute on PE1
/// assert_eq!(val, 1); // this was the second am to execute on PE1 so the fetched value is 1
/// }
/// //or we can iterate over the results
/// for res in results{
/// match res{
/// AmGroupResult::Pe((pe,val)) => { println!("{} from PE{}",val,pe)},
/// AmGroupResult::All(val) => { println!("{} on all PEs",val)},
/// }
/// }
/// }
/// }
///```
/// Expected output on each PE1:
/// ```text
/// 0 from PE0
/// [1,0] on all PEs
/// 1 from PE1
/// [2,2] on all PEs
/// ```
/// ### Static Members
/// In the above code, the `ExampleAm` stuct contains a member that is a [crate::darc::Darc](Darc) (Distributed Arc).
/// In order to properly calculate distributed reference counts Darcs implements specialized Serialize and Deserialize operations.
/// While, the cost to any single serialization/deserialization operation is small, doing this for every active message containing
/// a Darc can become expensive.
///
/// In certain cases Typed Am Groups can avoid the repeated serialization/deserialization of Darc members if the user guarantees
/// that every Active Message in the group is using a reference to the same Darc. In this case, we simply would only need
/// to serialize the Darc once for each PE it gets sent to.
///
/// This can be accomplished by using the [AmData] attribute macro with the `static` keyword passed in as an argument as illustrated below:
/// ```
/// use lamellar::active_messaging::prelude::*;
/// use lamellar::darc::prelude::*;
/// use std::sync::atomic::AtomicUsize;
/// #[AmData(Debug,Clone)]
/// struct ExampleAm {
/// #[AmData(static)]
/// cnt: Darc<AtomicUsize>,
/// }
///```
/// Other than the addition of `#[AmData(static)]` the rest of the code as the previous example would be the same.
#[proc_macro_error]
#[proc_macro]
pub fn typed_am_group(input: TokenStream) -> TokenStream {
// println!("typed_am_group {:?}",input);
let am_group: AmGroups = syn::parse(input).unwrap();
let am_type = am_group.am;
let team = am_group.team;
quote! {
#am_type::create_am_group(#team)
}
.into()
}